Variables help us store and remember information in a program. They let us use that information later or change it as needed.
Why variables are needed in Javascript
Introduction
Syntax
Javascript
let variableName = value;let is used to create a variable you can change later.
variableName is the name you give to the variable.
Examples
age and stores the number 25 in it.Javascript
let age = 25;
name.Javascript
let name = "Alice";
Javascript
let score = 0; score = score + 10;
Sample Program
This program stores a name and a number in variables. It then prints messages using those variables. It also shows how to change a variable's value.
Javascript
let userName = "Sam"; console.log("Hello, " + userName + "!"); let counter = 1; console.log("Counter is at: " + counter); counter = counter + 1; console.log("Counter is now: " + counter);
Important Notes
Variable names should be clear and describe what they hold.
You can change the value stored in a variable anytime.
Using variables makes your code easier to fix and update.
Summary
Variables store information to use later in your program.
They make your code flexible and easier to understand.
You can change what a variable holds as your program runs.
Practice
1. Why do we use variables in JavaScript?
let age = 25;easy
Solution
Step 1: Understand what variables do
Variables hold data that can be used and updated in the program.Step 2: Analyze the example
The codelet age = 25;stores the number 25 in the variable named age, which can be changed later.Final Answer:
To store information that can be used and changed later -> Option AQuick Check:
Variables store and change data [OK]
Hint: Variables hold data you want to reuse or change [OK]
Common Mistakes:
- Thinking variables make code run faster
- Confusing variables with comments
- Believing variables cannot change
2. Which of the following is the correct way to declare a variable in JavaScript?
easy
Solution
Step 1: Check JavaScript variable syntax
Variables can be declared usinglet,var, orconstfollowed by a valid name.Step 2: Identify valid variable names and syntax
let name = 'John';is correct because it useslet, a valid name, and proper assignment.Final Answer:
let name = 'John'; -> Option AQuick Check:
Correct variable declaration [OK]
Hint: Variable names can't start with numbers; use let/const/var [OK]
Common Mistakes:
- Starting variable names with numbers
- Using invalid keywords like 'variable'
- Incorrect assignment syntax
3. What will be the output of this code?
let score = 10; score = score + 5; console.log(score);
medium
Solution
Step 1: Understand variable update
The variablescorestarts at 10, then 5 is added to it.Step 2: Calculate the new value
10 + 5 equals 15, soscorebecomes 15.Final Answer:
15 -> Option BQuick Check:
10 + 5 = 15 [OK]
Hint: Add numbers step-by-step to find variable's new value [OK]
Common Mistakes:
- Forgetting variable was updated
- Confusing initial value with final
- Expecting undefined because of reassignment
4. Find the error in this code:
let total = 20 let total = 30; console.log(total);
medium
Solution
Step 1: Check variable declarations
The code declarestotaltwice usinglet, which is not allowed in the same scope.Step 2: Understand let rules
letvariables cannot be redeclared; you can only assign a new value withoutlet.Final Answer:
You cannot declare the same variable twice with let -> Option CQuick Check:
Redeclaring let variable causes error [OK]
Hint: Use let once per variable; assign new values without let [OK]
Common Mistakes:
- Thinking semicolon is mandatory for error
- Believing variable names are invalid
- Assuming console.log causes error
5. You want to store a user's name and age, then update the age after a birthday. Which code correctly uses variables for this?
??
hard
Solution
Step 1: Identify which variables can change
The user's name does not change, soconstis good. Age changes, soletis needed.Step 2: Check code correctness
const name = 'Alice'; let age = 30; age = age + 1; console.log(name, age); declaresnameasconstandageaslet, then updatesagecorrectly.Final Answer:
const name = 'Alice'; let age = 30; age = age + 1; console.log(name, age); -> Option DQuick Check:
Use const for fixed, let for changeable variables [OK]
Hint: Use const for fixed, let for values that change [OK]
Common Mistakes:
- Trying to change const variables
- Redeclaring variables with const or let
- Mixing let and const incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
