JavaScript Variables Practice Questions with Solutions

Variables are one of the most important concepts in JavaScript because they allow you to store and work with information. In this chapter, you will practice creating variables with let, const, and var, changing variable values, storing different types of data, and using variables in calculations and messages. The examples begin with simple tasks and gradually introduce practical situations that help beginners understand how JavaScript variables work. JavaScript Variables practice questions with solutions help to build concepts.

Question 1: Create and Display a Variable

Problem

Create a variable named studentName, store the value "Aman" in it, and display the value in the console.

Solution

letstudentName="Aman";console.log(studentName);

Output

Aman

Step-by-step Explanation

  1. let is used to create a variable.
  2. studentName is the variable name.
  3. "Aman" is stored in the variable.
  4. console.log(studentName) displays the stored value.

Question 2: Store a Student’s Age

Problem

Create a variable named age, store the value 15, and display it in the console.

Solution

letage=15;console.log(age);

Output

15

Step-by-step Explanation

  1. The let keyword creates the variable.
  2. The variable is named age.
  3. The number 15 is stored inside it.
  4. console.log() displays the value.

A number does not need quotation marks.


Question 3: Change the Value of a Variable

Problem

Create a variable named score with the value 50. Then change its value to 80 and display the new value.

Solution

letscore=50;score=80;console.log(score);

Output

80

Step-by-step Explanation

  1. let score = 50 creates the variable with an initial value of 50.
  2. score = 80 changes the value stored in the variable.
  3. The variable now contains 80.
  4. console.log(score) displays 80.

A variable created with let can be reassigned.


Question 4: Use Two Variables in a Calculation

Problem

Create two variables named price and quantity. Store 100 in price and 3 in quantity. Calculate and display the total cost.

Solution

letprice=100;letquantity=3;lettotal=price*quantity;console.log(total);

Output

300

Step-by-step Explanation

  1. price stores 100.
  2. quantity stores 3.
  3. The * operator multiplies the two values.
  4. 100 × 3 = 300.
  5. The result is stored in total.
  6. console.log(total) displays 300.

Question 5: Create a Constant

Problem

Create a constant named country with the value "India" and display it.

Solution

constcountry="India";console.log(country);

Output

India

Step-by-step Explanation

  1. const is used to create a constant variable.
  2. country is the variable name.
  3. "India" is stored in the variable.
  4. console.log(country) displays the value.

A const variable cannot be reassigned after it has been created.


Question 6: Calculate a Student’s Age

Problem

Create a variable named birthYear with the value 2011 and another variable named currentYear with the value 2026. Calculate the student’s age.

Solution

letbirthYear=2011;letcurrentYear=2026;letage=currentYear-birthYear;console.log(age);

Output

15

Step-by-step Explanation

  1. birthYear stores 2011.
  2. currentYear stores 2026.
  3. The current year is subtracted from the birth year in the required order.
  4. 2026 - 2011 = 15.
  5. The result is stored in age.
  6. The program displays 15.

Question 7: Combine Variables to Create a Message

Problem

Create two variables, firstName and lastName, and display the person’s full name.

Solution

letfirstName="Rahul";letlastName="Sharma";letfullName=firstName+" "+lastName;console.log(fullName);

Output

Rahul Sharma

Step-by-step Explanation

  1. firstName stores "Rahul".
  2. lastName stores "Sharma".
  3. The + operator joins the values.
  4. " " adds a space between the two names.
  5. The complete name is stored in fullName.
  6. console.log(fullName) displays the result.

Question 8: Calculate the Remaining Money

Problem

A student has ₹500 and spends ₹175. Create variables for the available money and spending, then calculate the remaining money.

Solution

letmoney=500;letspent=175;letremaining=money-spent;console.log(remaining);

Output

325

Step-by-step Explanation

  1. money stores 500.
  2. spent stores 175.
  3. The - operator subtracts the spending from the available money.
  4. 500 - 175 = 325.
  5. The result is stored in remaining.
  6. The program displays 325.

Question 9: Use a Variable to Update a Score

Problem

A player starts with a score of 100. The player earns another 50 points. Update the score and display the new score.

Solution

letscore=100;score=score+50;console.log(score);

Output

150

Step-by-step Explanation

  1. The variable score initially contains 100.
  2. score + 50 calculates 150.
  3. The result is assigned back to score.
  4. The variable now contains 150.
  5. console.log(score) displays the updated score.

You can also write the same operation using the += operator:

score+=50;

Question 10: Compare let, const and var

Problem

Create three variables using let, const, and var. Store a city name in each variable and display all three values.

Solution

letcity1="Delhi";constcity2="Mumbai";varcity3="Jaipur";console.log(city1);console.log(city2);console.log(city3);

Output

DelhiMumbaiJaipur

Step-by-step Explanation

  1. let city1 creates a variable using let.
  2. const city2 creates a constant using const.
  3. var city3 creates a variable using the older var keyword.
  4. All three variables contain string values.
  5. console.log() displays each value.

For modern JavaScript, let and const are generally preferred over var.

Key Takeaways

  • A variable is used to store information in a JavaScript program.
  • let is used when a variable’s value may need to change.
  • const is used when a variable should not be reassigned.
  • var is an older way of declaring variables.
  • Variables can store strings, numbers, booleans, and other types of values.
  • A variable created with let can be reassigned.
  • A const variable cannot be reassigned.
  • Variables can be used in mathematical calculations.
  • Variables can be combined to create useful messages.
  • += can be used to add a value to an existing variable.
  • Choosing meaningful variable names makes code easier to understand.

FAQs

1. What is a variable in JavaScript?

A variable is a named storage location that allows a program to store and use a value.

2. What is the difference between let and const?

let allows a variable to be reassigned, while const does not allow the variable to be reassigned after declaration.

3. What is var in JavaScript?

var is an older keyword used to declare variables. Modern JavaScript code generally prefers let and const.

4. Can a let variable be changed?

Yes. A variable declared with let can be assigned a new value.

letage=14;age=15;console.log(age);

5. Can a const variable be changed?

A const variable cannot be reassigned after it has been declared.

constcountry="India";

Trying to assign another value to country will cause an error.

6. What should I use: let or const?

Use const when you do not need to reassign the variable. Use let when the value needs to change.

7. Can JavaScript variables store different types of values?

Yes. JavaScript variables can store different types of values, including strings, numbers, booleans, objects, arrays, and more.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top