JavaScript Basics: Practice Questions and Exercises
Variables
Practice Questions:
• 1. What is a variable in JavaScript?
• 2. How do you declare a variable using var, let, and const?
• 3. What is the difference between var, let, and const?
• 4. Can you reassign a value to a const variable?
• 5. What are the rules for naming variables in JavaScript?
Exercises:
• 1. Declare a variable named 'city' and assign your hometown to it.
• 2. Create a variable 'age' using let and update its value later.
• 3. Try to reassign a const variable and observe the error.
• 4. Create three variables using var, let, and const and print their values in the
console.
Data Types
Practice Questions:
• 1. What are the primitive data types in JavaScript?
• 2. How do you check the type of a variable?
• 3. What is the difference between null and undefined?
• 4. What is NaN and how can you check if a value is NaN?
• 5. Explain the difference between == and === operators.
Exercises:
• 1. Create variables of types string, number, boolean, null, and undefined.
• 2. Use typeof operator to check each variable’s type.
• 3. Write a program that compares '5' and 5 using both == and ===.
• 4. Create a variable with NaN value and use isNaN() to check it.
Operators
Practice Questions:
• 1. What are arithmetic operators in JavaScript?
• 2. Explain the difference between == and === operators.
• 3. What is the purpose of the typeof operator?
• 4. What are logical operators in JavaScript?
• 5. What is the difference between ++i and i++?
Exercises:
• 1. Write a program to add, subtract, multiply, and divide two numbers.
• 2. Use logical operators to check if a number is between 10 and 20.
• 3. Demonstrate the difference between pre-increment and post-increment.
• 4. Use ternary operator to print 'Adult' or 'Minor' based on age.
Conditional Statements
Practice Questions:
• 1. What are conditional statements in JavaScript?
• 2. Explain the syntax of if, else if, and else statements.
• 3. What is a ternary operator and how is it used?
• 4. Can you nest if statements in JavaScript?
• 5. How does the switch statement work? Provide an example.
Exercises:
• 1. Write a program to check if a number is positive, negative, or zero.
• 2. Use if...else to determine if a number is even or odd.
• 3. Write a program using switch to print day of the week based on a number
(1-7).
• 4. Use ternary operator to check if a number is greater than 100.
Loops
Practice Questions:
• 1. What are loops used for in JavaScript?
• 2. Explain the difference between for, while, and do...while loops.
• 3. What is the purpose of the break and continue statements?
• 4. How can you iterate over an array using a for loop?
• 5. What is a for...of loop and when should you use it?
Exercises:
• 1. Write a for loop to print numbers 1 to 10.
• 2. Use a while loop to print even numbers between 1 and 20.
• 3. Use a for...of loop to print all elements of an array.
• 4. Write a loop that calculates the sum of numbers from 1 to 100.
• 5. Use break and continue in a loop and observe their behavior.
Functions
Practice Questions:
• 1. What is a function in JavaScript?
• 2. How do you declare and call a function?
• 3. What is the difference between a function declaration and a function
expression?
• 4. What are arrow functions? Give an example.
• 5. What is the purpose of the return statement in a function?
Exercises:
• 1. Write a function that prints 'Hello, World!'.
• 2. Create a function that takes two numbers and returns their sum.
• 3. Write a function to check if a number is prime.
• 4. Convert a normal function to an arrow function.
• 5. Write a function that returns the factorial of a number.