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)

  1. Declare a variable name and assign your full name to it. What is the data type?
  2. Create a variable age and assign your age. Is it a string or a number?
  3. Declare a variable isStudent and assign it the value true. What is the type of this variable?
  4. What will be the value and type of the following variable?
    let score;
    
  5. What is the output of the following code?
    let city = "Delhi";
    console.log(typeof city);
    
  6. Write a program that declares 3 variables: firstName (string), age (number), and isMarried (boolean). Log them all to the console.
  7. What is the output and type of:
    let x = 42 + "7";
    console.log(x);
    
  8. Change the value of the variable x below and log the result.
    let x = 10;
    x = "ten";
    console.log(x);
    
  9. Declare a constant PI with the value 3.14. Try to reassign it. What happens?
  10. Identify the error:
    const myName;
    myName = "John";
    

🔄 Intermediate Level Questions (11–20)

  1. Check the type of the following values using typeof:
    • null
    • undefined
    • NaN
    • "123"
    • 123
  2. Convert the string "100" to a number and add 50 to it.
  3. What is the output of:
    let a = "5";
    let b = 2;
    console.log(a * b);
    
  4. Create a variable and assign the result of true + false. What do you get and why?
  5. Write a script that asks the user for their name using prompt() and then logs: "Hello, <name>!".
  6. What is the difference between var, let, and const in variable declarations? Give an example.
  7. Predict the output:
    let a = "10";
    let b = 10;
    console.log(a == b);   // ?
    console.log(a === b);  // ?
    
  8. Write a program to swap the values of two variables without using a third variable.
  9. Use template literals to log a sentence: My name is <name> and I am <age> years old.
  10. 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.