Skip to main content

Variables and Data Types

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

KeywordScopeReassignableHoistedUse When
varFunctionYesYesLegacy code only
letBlockYesNoValue will change
constBlockNoNoValue 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.

Ctrl+Enter
HTML
CSS
JS
Preview

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.

Ctrl+Enter
HTML
CSS
JS
Preview

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.

Ctrl+Enter
HTML
CSS
JS
Preview

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.

Ctrl+Enter
HTML
CSS
JS
Preview

Falsy Values

These values all evaluate to false in a boolean context:

false, 0, -0, 0n, "", null, undefined, NaN

Everything else is truthy — including empty arrays [] and empty objects {}.

Strict vs Loose Equality

Always use strict equality (===) instead of loose equality (==).

Ctrl+Enter
HTML
CSS
JS
Preview

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.

Ctrl+Enter
HTML
CSS
JS
Preview

If you need a truly immutable object, use Object.freeze().

Ctrl+Enter
HTML
CSS
JS
Preview

Practical Exercise

Create a small user profile using different data types:

Ctrl+Enter
HTML
CSS
JS
Preview

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 const by default, let when reassignment is needed, and avoid var.
  • 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.
  • const prevents reassignment but does not make objects or arrays immutable.