30 JavaScript Basic Exercises for Beginners with Solutions
Master beginner JavaScript skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in JavaScript, setting a solid foundation for intermediate challenges. Start your journey to JavaScript mastery today!
Learning Objectives:
By the end of these exercises, you will have a foundational understanding of JavaScript basics, including variables, functions, loops, and basic DOM manipulation.
Exercise Instructions:
- Start with the first exercise and attempt to solve it before checking the hint or solution.
- Ensure you understand the logic behind each solution, as this will help you in more complex problems.
- Use these exercises to reinforce your learning and identify areas that may require further study.
1. Print "Hello, World!" to the console.
Required Input:
"Hello, World!"
Expected Output:
Hello, World!
Code In Javascript
Run Code?
Click Run Button to view compiled output
2. Declare a variable name and assign it your name. Print the variable.
Required Input:
"Alice"
Expected Output:
Alice
Code In Javascript
Run Code?
Click Run Button to view compiled output
3. Create two variables, a and b, assign them numbers, and print their sum.
Required Input:
a = 5, b = 10
Expected Output:
15
Code In Javascript
Run Code?
Click Run Button to view compiled output
4. Write a program to find the length of a string stored in a variable str.
Required Input:
str = "Hello"
Expected Output:
5
Code In Javascript
Run Code?
Click Run Button to view compiled output
5. Create a variable num and check if it is even or odd.
Required Input:
num = 7
Expected Output:
Odd
Code In Javascript
Run Code?
Click Run Button to view compiled output
6. Write a program to calculate the area of a rectangle given length and width.
Required Input:
length = 5, width = 10
Expected Output:
50
Code In Javascript
Run Code?
Click Run Button to view compiled output
7. Create a function greet that accepts a name as a parameter and returns "Hello, [name]!"
Required Input:
name = "Alice"
Expected Output:
Hello, Alice!
Code In Javascript
Run Code?
Click Run Button to view compiled output
8. Write a program to convert temperature from Celsius to Fahrenheit.
Required Input:
celsius = 30
Expected Output:
86
Code In Javascript
Run Code?
Click Run Button to view compiled output
9. Create a function to find the maximum of three numbers.
Required Input:
a = 3, b = 9, c = 6
Expected Output:
9
Code In Javascript
Run Code?
Click Run Button to view compiled output
10. Write a program that checks if a string is a palindrome.
Required Input:
str = "madam"
Expected Output:
true
Code In Javascript
Run Code?
Click Run Button to view compiled output