Introductions
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
letis used to create a variable.studentNameis the variable name."Aman"is stored in the variable.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
- The
letkeyword creates the variable. - The variable is named
age. - The number
15is stored inside it. 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
let score = 50creates the variable with an initial value of50.score = 80changes the value stored in the variable.- The variable now contains
80. console.log(score)displays80.
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
pricestores100.quantitystores3.- The
*operator multiplies the two values. 100 × 3 = 300.- The result is stored in
total. console.log(total)displays300.
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
constis used to create a constant variable.countryis the variable name."India"is stored in the variable.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
birthYearstores2011.currentYearstores2026.- The current year is subtracted from the birth year in the required order.
2026 - 2011 = 15.- The result is stored in
age. - 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
firstNamestores"Rahul".lastNamestores"Sharma".- The
+operator joins the values. " "adds a space between the two names.- The complete name is stored in
fullName. 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
moneystores500.spentstores175.- The
-operator subtracts the spending from the available money. 500 - 175 = 325.- The result is stored in
remaining. - 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
- The variable
scoreinitially contains100. score + 50calculates150.- The result is assigned back to
score. - The variable now contains
150. 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
let city1creates a variable usinglet.const city2creates a constant usingconst.var city3creates a variable using the oldervarkeyword.- All three variables contain string values.
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.
letis used when a variable’s value may need to change.constis used when a variable should not be reassigned.varis an older way of declaring variables.- Variables can store strings, numbers, booleans, and other types of values.
- A variable created with
letcan be reassigned. - A
constvariable 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.
