Every program needs to store and manipulate data. In JavaScript, you do this with variables. This lesson covers how to declare variables and the different data types you will work with daily.
Declaring Variables
JavaScript has three keywords for declaring variables: var, let, and const.
var name = "Sabaoon"; // function-scoped, avoid in modern code
let age = 25; // block-scoped, can be reassigned
const PI = 3.14159; // block-scoped, cannot be reassigned
When to Use Each
| Keyword | Scope | Reassignable | Hoisted | Use When |
|---|---|---|---|---|
var | Function | Yes | Yes | Legacy code only |
let | Block | Yes | No | Value will change |
const | Block | No | No | Value stays the same (default) |
Best practice: Default to const. Only use let when you know the value needs to change. Avoid var entirely in modern JavaScript.
Primitive Data Types
JavaScript has seven primitive types. The ones you will use most often are:
String
Strings represent text. You can create them with single quotes, double quotes, or backticks.
const greeting = "Hello";
const name = 'World';
const message = `${greeting}, ${name}!`; // template literal
console.log(message); // "Hello, World!"
Template literals (backticks) let you embed expressions with ${} and span multiple lines.
Number
JavaScript uses a single Number type for both integers and decimals.
Watch out for floating-point precision issues when working with money. Use integers (cents) or a library like decimal.js for financial calculations.
Boolean
Booleans are either true or false.
Null and Undefined
These two types represent the absence of a value, but in different ways.
let user = null; // explicitly set to "no value"
let score; // undefined — declared but not assigned
console.log(user); // null
console.log(score); // undefined
Use null when you intentionally want to indicate "no value." undefined usually means a variable has not been assigned yet.
Type Checking with typeof
The typeof operator returns a string describing the type of a value.
Note that typeof null returns "object" — this is a well-known bug in JavaScript that will never be fixed for backward compatibility reasons.
Type Coercion
JavaScript automatically converts types in certain situations. This is called coercion.
Falsy Values
These values all evaluate to false in a boolean context:
false, 0, -0, 0n, "", null, undefined, NaNEverything else is truthy — including empty arrays [] and empty objects {}.
Strict vs Loose Equality
Always use strict equality (===) instead of loose equality (==).
Loose equality applies type coercion before comparing, which leads to surprising results. Strict equality checks both value and type.
Constants and Immutability
const prevents reassignment but does not make objects immutable.
If you need a truly immutable object, use Object.freeze().
Practical Exercise
Create a small user profile using different data types:
The nullish coalescing operator (??) returns the right side only when the left side is null or undefined — unlike || which also triggers on 0, "", and false.
Key Takeaways
- Use
constby default,letwhen reassignment is needed, and avoidvar. - JavaScript has seven primitive types; the most common are string, number, boolean, null, and undefined.
- Always use
===for comparisons to avoid type coercion surprises. - Template literals with backticks make string interpolation clean and readable.
constprevents reassignment but does not make objects or arrays immutable.