What are variables?

A variable is a container for storing data. Think of it like a labeled box in a warehouse. The label is the variable name, and whatever you put inside the box is the value.

In an e-commerce app like UrbanCart, you need to remember things: the product name, its price, how many are in stock, whether the customer is logged in. Variables hold all of that.

๐Ÿ“ฆ
UrbanCart Example
Variables are labeled boxes for your data

Imagine UrbanCart's warehouse. One box is labeled "productName" and contains "Wireless Earbuds". Another is labeled "price" and contains 2499. A third is labeled "inStock" and contains true. That's exactly how JavaScript variables work.

variables.js Your first variables
Console output
Wireless Earbuds 2499 true
Naming rules: Variable names must start with a letter, underscore (_), or dollar sign ($). They are case-sensitive (price and Price are different). Use camelCase for multi-word names: productName, cartTotal, isLoggedIn.

var vs let vs const

JavaScript has three keywords for declaring variables. Understanding the differences is essential โ€” and the rule is simple: always prefer const, use let when you need reassignment, and avoid var.

Feature var let const
Scope Function-scoped Block-scoped Block-scoped
Hoisted? Yes (undefined) No (TDZ) No (TDZ)
Reassignable? Yes Yes No
Redeclarable? Yes (bug-prone) No No
Recommendation Avoid Use when needed Default choice

var โ€” The old way (avoid it)

var is function-scoped, meaning it ignores block boundaries like if and for. It's also hoisted โ€” you can use it before it's declared (it just returns undefined). This causes subtle bugs.

var_problems.js Why var is problematic
Console output
I escaped the if-block! 0

let โ€” Reassignable, block-scoped

Use let when the value needs to change โ€” like a cart count or a running total.

let_example.js Use let for values that change
Console output
Items in cart: 2

const โ€” Your default choice

Use const for values that shouldn't change โ€” store names, tax rates, configuration. This is your default. Only switch to let if you truly need reassignment.

const_example.js const is your default
Console output
UrbanCart 0.18 50
Golden rule: Start with const. If you later need to reassign the variable, change it to let. Never use var in modern JavaScript. This single habit prevents a whole class of bugs.

Data types

Every value in JavaScript has a type. JavaScript has 7 primitive types, but you'll use these 5 most often. Let's explore each one with UrbanCart examples.

string โ€” Text data

Strings hold text. Wrap them in single quotes ('...'), double quotes ("..."), or backticks (`...`). All three work, but be consistent.

strings.js Text values in UrbanCart
Console output
Wireless Earbuds Electronics Noise-cancelling, 24hr battery string

number โ€” Numeric data

JavaScript has a single number type for both integers and decimals. No need to choose between int, float, or double โ€” JS handles it all.

numbers.js Prices, quantities, ratings
Console output
2499 99.5 number number

boolean โ€” true or false

Booleans represent yes/no, on/off, true/false decisions. They're the backbone of conditional logic โ€” "Is this product in stock?" "Is the user logged in?"

booleans.js Yes/No decisions
Console output
true false boolean boolean

null and undefined

These two represent "nothing," but in different ways:

  • undefined โ€” the variable exists but hasn't been assigned a value yet. JavaScript sets this automatically.
  • null โ€” you intentionally set a variable to "no value." It's a deliberate choice.
null_undefined.js The two flavors of "nothing"
Console output
undefined undefined null object
Fun quirk: typeof null returns "object". This is a famous JavaScript bug from 1995 that was never fixed because too much existing code depends on it. Just remember: null is NOT an object.

typeof โ€” Check any value's type

The typeof operator tells you what type a value is. It returns a string.

typeof_examples.js Inspect your data types
Console output
string number boolean undefined object function

Template literals

Template literals use backticks (`...`) instead of quotes, and let you embed expressions inside ${...}. This is called interpolation โ€” and it's far cleaner than concatenating strings with +.

template_literals.js String interpolation with backticks
Console output
Welcome to UrbanCart! Your total is $4998 Welcome to UrbanCart! Your total is $4998 You have 3 items, average: $1666.00 Order Summary โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Items: 3 Total: $4998 Store: UrbanCart
Always use template literals when you need to mix variables with text. They're more readable, support multi-line strings, and let you embed any JavaScript expression inside ${...}.

Type coercion

JavaScript is loosely typed โ€” it will automatically convert types when it thinks it should. This leads to some surprising behavior. Understanding coercion prevents some of the most common bugs.

The + operator โ€” string wins

When you use + with a string and a number, JavaScript converts the number to a string and concatenates:

coercion_plus.js String + Number = String (concatenation)
Console output
"53" "Price: 99" "33" "321"

Other operators โ€” number wins

With -, *, /, JavaScript converts strings to numbers:

coercion_math.js String - Number = Number
Console output
2 20 25 NaN

== vs === (Always use ===)

This is one of the most important rules in JavaScript:

  • == (loose equality) โ€” converts types before comparing. Avoid it.
  • === (strict equality) โ€” no conversion, both type AND value must match. Always use this.
equality.js Always use === (strict equality)
Console output
true true true true false false false false
Rule: Always use === and !==. Never use == or !=. The loose versions cause subtle, hard-to-debug issues. Every professional JavaScript team enforces this via linting rules.

Try it yourself โ€” Product variables

Create variables for an UrbanCart product: name, price, inStock, and rating. Use const for each. Log them all to the console.

โœ๏ธ Try It Yourself โ€” Product Variables
Output

Try it yourself โ€” Product summary

Use template literals to build a product summary string. Combine the product name, price, rating, and stock status into a readable message.

โœ๏ธ Try It Yourself โ€” Template Literal
Output

Test your knowledge

Question 1 of 5
Which keyword should you use by default to declare a variable in modern JavaScript?
Question 2 of 5
What does "5" + 3 evaluate to in JavaScript?
Question 3 of 5
What is the difference between null and undefined?
Question 4 of 5
What does 5 === "5" return?
Question 5 of 5
Which syntax correctly creates a template literal with variable interpolation?
0
/5
Great job!
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You now speak JavaScript's language.
Let's build your career.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’