20 JavaScript Practice Questions
Last Updated: June 11, 2025
Here are 20 JavaScript practice questions focused on variables and data types. These questions range from beginner to intermediate level to help reinforce understanding.
🔰 Basic Level Questions (1–10)
- Declare a variable
nameand assign your full name to it. What is the data type? - Create a variable
ageand assign your age. Is it a string or a number? - Declare a variable
isStudentand assign it the valuetrue. What is the type of this variable? - What will be the value and type of the following variable?
let score; - What is the output of the following code?
let city = "Delhi"; console.log(typeof city); - Write a program that declares 3 variables:
firstName(string),age(number), andisMarried(boolean). Log them all to the console. - What is the output and type of:
let x = 42 + "7"; console.log(x); - Change the value of the variable
xbelow and log the result.let x = 10; x = "ten"; console.log(x); - Declare a constant
PIwith the value3.14. Try to reassign it. What happens? - Identify the error:
const myName; myName = "John";
🔄 Intermediate Level Questions (11–20)
- Check the type of the following values using
typeof:nullundefinedNaN"123"123
- Convert the string
"100"to a number and add 50 to it. - What is the output of:
let a = "5"; let b = 2; console.log(a * b); - Create a variable and assign the result of
true + false. What do you get and why? - Write a script that asks the user for their name using
prompt()and then logs:"Hello, <name>!". - What is the difference between
var,let, andconstin variable declarations? Give an example. - Predict the output:
let a = "10"; let b = 10; console.log(a == b); // ? console.log(a === b); // ? - Write a program to swap the values of two variables without using a third variable.
- Use template literals to log a sentence:
My name is <name> and I am <age> years old. - Write a function
describePerson(name, age, isStudent)that returns a sentence like:"John is 25 years old and is a student"or"Sarah is 30 years old and is not a student"based on the boolean.
