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.
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.
_), 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.
let โ Reassignable, block-scoped
Use let when the value needs to change โ like a cart count or a running total.
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. 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.
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.
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?"
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.
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.
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 +.
${...}.
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:
Other operators โ number wins
With -, *, /, JavaScript converts strings to numbers:
== 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.
=== 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 summary
Use template literals to build a product summary string. Combine the product name, price, rating, and stock status into a readable message.
Test your knowledge
"5" + 3 evaluate to in JavaScript?null and undefined?5 === "5" return?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 โ