Sitemap

JavaScript Variables and Data Types

2 min readJul 3, 2025

--

JavaScript Variables

JavaScript provides mainly three ways to initialize variables, let, const and var. Each way have its own use cases and properties

Var

Syntax:

var name;

//or you can directly assign value like this

var name = "John doe";

Properties:

  • Var have global scope.
  • It supports hoisting. Hoisting moves all the declaration of variables or functions to the start of scope.
  • It can be redeclared or value can be reassigned/changed
//Hoisting example
console.log(name); //output - undefined
var name = "John";

//the above code will be equivalent to following code because of hoisting

var name = undefined;
console.log(name);
name = "John";

Use of var is not recommended on modern code. Instead use let for better code and usability.

Let

Syntax:

let name;

//or you can directly assign value

let name = "John";

Properties:

  • Let have block scope.
  • The declarations are hoisted, but not initialized. The variables remain in the temporal dead zone from the start of the block until their initialization line is executed.
  • It gives error if redeclared within its scope but we can reassign value.

Const

Syntax:

//unlike var and let, const must be initialized
const PI = 3.1415;

Properties:

  • All properties are same as Let except once assigned, the value cannot be changed.

Data Types

JavaScript values are categorized into primitive and reference types, each with distinct memory and assignment behaviors.

Get Binod’s stories in your inbox

Join Medium for free to get updates from this writer.

Primitive Types

Primitive types are:

  • Number
  • String
  • Boolean
  • Undefined
  • Null
  • Symbol
  • BigInt

Characteristics:

  • Stored by value: Assigning a primitive to another variable copies the value.
  • Immutable: Once created, their value cannot be changed (though variables can be reassigned to new values).
  • Stored in stack memory: Each variable has its own space on the stack.

Example:

let a = 10;
let b = a; // b = 10
a = 20;
console.log(b); // 10 (b is unaffected by changes to a)

Changing a does not affect b because the value was copied.

Reference Types

Reference types include:

  • Objects
  • Arrays
  • Functions

Characteristics:

  • Stored by reference: Variables store a pointer (reference) to the actual value in heap memory.
  • Mutable: The contents can be changed, even if the reference is held by a const variable.
  • Assignment copies the reference: Multiple variables can point to the same object, so changes via one variable are visible to others7856.

Example:

let obj1 = { name: "Alice" };
let obj2 = obj1;
obj2.name = "Bob";
console.log(obj1.name); // "Bob" (both reference the same object)

Both obj1 and obj2 point to the same object in memory, so modifying through one affects the other.

--

--