Variables
A variable in JavaScript is a named container for storing a data value, declared with let , const , or var , so you can label, reuse, and update information in your program.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free JavaScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Learn how to store, use, and manage all kinds of information inside your programs.
💡 Running Code Locally: While this online editor runs real JavaScript, some advanced examples may have limitations. For the best experience:
📦 Real-World Analogy: Variables are like labeled storage boxes :
A variable is like a box with a label — you can store data inside it and use it later in your program. Imagine needing to remember a user's name, score, or age — instead of retyping the number or word every time, you store it in a variable.
When you create ( declare ) a variable, you're telling the computer:
There are three main ways to declare variables in JavaScript:
Avoid using var . It still works but can cause bugs due to outdated behavior. Always use let or const in modern JavaScript.
A data type defines what kind of data a variable holds — text, number, true/false, etc.
JavaScript has seven main primitive types you'll use daily:
Let's explore the common ones for beginners 👇
💡 You can use: "Double quotes" , 'Single quotes' , or
💡 JavaScript treats both integers and decimals as the same number type.
You'll use booleans for decisions and conditions, like checking if a user is logged in.
• undefined — variable exists but has no value yet • null — means "nothing", used intentionally
Template literals (with backticks) let you embed variables directly into strings:
This is easier to read and write than concatenation like:
To check what type of data a variable holds, use the typeof operator:
JavaScript is flexible — you can combine strings and numbers easily:
In JavaScript, you can change a variable's data type anytime:
That's because JavaScript is a dynamically typed language — it decides the type automatically at runtime.
When naming variables, follow these simple rules and conventions:
✅ Use const for values that never change, like API keys or settings.
✅ Use let for values that change often, like scores or totals.
Let's simulate a small "profile card" using variables:
Now imagine this is part of your social app (like Flick 😉) — variables store dynamic data that updates automatically when a user gets new followers.
JavaScript automatically converts between numbers and strings when needed — this is called type coercion .
It can be confusing at first, so always use correct types when doing math!
Create variables to store information about a book :
Then, use template literals to create and display a sentence describing the book.
Add one more variable for price and use all values to print a full bookstore-style description.
If your program were a backpack, variables are the items you carry — text, numbers, or data you'll use later. The better you name and organize them, the easier your "journey" through coding becomes!
You now know how to store any kind of data — text, numbers, booleans — and build readable strings with template literals.
Up next: JavaScript Functions — how to group and reuse logic like a professional! 🔁
Practice quiz
Which keyword declares a variable whose value can be reassigned later?
- const
- let
- fixed
- define
Answer: let. let declares a reassignable, block-scoped variable; const cannot be reassigned.
What happens if you try to reassign a const variable?
- It silently updates
- It throws an error
- It creates a new variable
- It becomes undefined
Answer: It throws an error. const variables cannot be reassigned — doing so throws a TypeError.
Which of these is NOT one of JavaScript's seven primitive data types listed in the lesson?
- String
- Boolean
- Array
- BigInt
Answer: Array. Array is an object, not a primitive. The seven primitives are String, Number, Boolean, Undefined, Null, Symbol, and BigInt.
What does console.log(typeof age) print when age = 25?
- 'integer'
- 'number'
- '25'
- 'numeric'
Answer: 'number'. JavaScript treats all numbers (integers and decimals) as the 'number' type.
What does console.log("5" + 5) output?
- 10
- '55'
- 55
- NaN
Answer: '55'. The + operator with a string coerces the number to a string, giving '55'.
What does console.log("5" - 2) output?
- '52'
- 3
- '3'
- NaN
Answer: 3. The - operator coerces the string '5' to a number, so the result is 3.
Which symbols can you use to write template literals?
- Double quotes
- Single quotes
- Backticks
- Parentheses
Answer: Backticks. Template literals use backticks and allow ${...} interpolation.
What is the value of a variable that is declared but not assigned, e.g. let notDefined;?
- null
- 0
- undefined
- ''
Answer: undefined. A declared-but-unassigned variable holds the value undefined.
Which variable name is INVALID in JavaScript?
- _count
- $price
- user123
- 2name
Answer: 2name. Variable names cannot start with a number, so 2name is invalid.
Are the variables name and Name considered the same in JavaScript?
- Yes, names are case-insensitive
- No, names are case-sensitive
- Only with var
- Only inside functions
Answer: No, names are case-sensitive. JavaScript identifiers are case-sensitive, so name and Name are different variables.