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.
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.
What You'll Learn in This Lesson
💡 Running Code Locally: While this online editor runs real JavaScript, some advanced examples may have limitations. For the best experience:
- Download Node.js to run JavaScript on your computer
- Use your browser's Developer Console (Press F12) to test code snippets
- Create a .html file with <script> tags and open it in your browser
📦 Real-World Analogy: Variables are like labeled storage boxes :
- • The label (variable name) tells you what's inside
- • The contents (value) is what's actually stored
- • You can open the box anytime to use what's inside
- • With let , you can swap the contents; with const , the box is sealed!
🧠 What Are Variables?
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.
⚙️ How Variables Work
When you create ( declare ) a variable, you're telling the computer:
There are three main ways to declare variables in JavaScript:
1️⃣ let
2️⃣ const
3️⃣ var
Avoid using var . It still works but can cause bugs due to outdated behavior. Always use let or const in modern JavaScript.
⚖️ Comparison Table
Keyword
Reassignable?
Scope
Recommended?
let
✅ Yes
Block
✅ Best for most variables
const
❌ No
✅ Use when value never changes
var
Function
❌ Avoid (legacy only)
🧩 Example: Managing Variables
🟨 Data Types in 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:
- String — Text (words or sentences)
- Number — Any numerical value
- Boolean — True or False
- Undefined — Declared but not assigned
- Null — Empty or intentionally nothing
- Symbol — Unique identifier (advanced)
- BigInt — Very large integers
Let's explore the common ones for beginners 👇
🔤 Strings
💡 You can use: "Double quotes" , 'Single quotes' , or
🔢 Numbers
💡 JavaScript treats both integers and decimals as the same number type.
⚖️ Booleans
You'll use booleans for decisions and conditions, like checking if a user is logged in.
🕳️ Null & Undefined
• undefined — variable exists but has no value yet • null — means "nothing", used intentionally
🎯 Template Literals
Template literals (with backticks) let you embed variables directly into strings:
This is easier to read and write than concatenation like:
🧠 Type Checking
To check what type of data a variable holds, use the typeof operator:
🧮 Mixing Data Types
JavaScript is flexible — you can combine strings and numbers easily:
⚙️ Dynamic Typing
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.
🧱 Variable Naming Rules
When naming variables, follow these simple rules and conventions:
🧩 Best Practices
✅ Use const for values that never change, like API keys or settings.
✅ Use let for values that change often, like scores or totals.
🧮 Common Mistakes to Avoid
- 1️⃣ Forgetting to declare with let or const
- 2️⃣ Mixing cases
- 3️⃣ Redeclaring a const variable
- 4️⃣ Using reserved words (like if , for , return ) as variable names.
🧠 Fun Example — Real-World Simulation
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.
🧩 Example: Mini Calculator
💡 Did You Know?
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!
🎯 Practice Challenge
Create variables to store information about a book :
- Title (string)
- Author (string)
- Year published (number)
- Is available (boolean)
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.
💬 Recap
- ✅ Variables store data like numbers, text, and booleans
- ✅ let changes, const stays constant
- ✅ JavaScript data types include string, number, boolean, null, undefined
- ✅ Follow naming conventions and avoid reserved keywords
- ✅ Use template literals for cleaner, modern code
🧠 Try This Thought:
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!
📋 Quick Reference — Variables & Data Types
Use When
Value changes over time
Value never changes
Avoid — legacy only
Template literal
typeof
typeof name // "string"
Lesson 2 Complete — Variables & Data Types!
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.
Continue this course
- Previous: Introduction to JavaScript
- Next: Functions