Skip to main content
Madhukar
All Articles

Understanding Variables and Data Types in JavaScript

March 16, 20264 min read
javascriptbeginnervariablesbasics
Understanding Variables and Data Types in JavaScript

What is a variable and why we need them

A variable is a named container for a value. Without variables your code would be one long list of numbers and words you couldn’t refer to again. With variables you can:

  • store user input (name, age)
  • compute results and keep them
  • make your code readable and maintainable

Real-life analogy: label a box age and put a number in it. Later you can open the box, change the number, or use it in calculations.

How to declare variables: var, let, const

Short summary:

  • var — old style, function-scoped, avoid for beginners.
  • let — modern, block-scoped, you can change its value (reassign).
  • const — modern, block-scoped, value cannot be reassigned (the variable binding is constant).

Examples:

// var (older style — function scoped)
var name = "Madhukar";
console.log(name); // Madhukar

// let (block scoped, reassignable)
let age = 20;
console.log(age); // 20
age = 21;
console.log(age); // 21

// const (block scoped, not reassignable)
const isStudent = true;
console.log(isStudent); // true
// isStudent = false; // Error: Assignment to constant variable.

Primitive data types (simple definitions + examples)

1. String — text.
Example: “Alice”, “hello”

2. Number — integers or decimals.
Example: 42, 3.14

3. Boolean — true or false.
Example: true, false

4. Null — deliberate absence of a value.
Example:
let x = null;

5. Undefined — variable declared but not given a value.
Example:
let y;

6. BigInt — represents integers larger than JavaScript’s safe integer limit (`Number.MAX_SAFE_INTEGER`).
Example:
let bigNumber = 9007199254740993n;

7. Symbol — represents a unique and immutable value, commonly used as a unique object property key.
Example:
const uniqueId = Symbol(“id”);

let name = "Alice";               // string
let score = 95.5; // number
let passed = true; // boolean
let nothing = null; // null
let notSet; // undefined
let bigNumber = 9007199254740993n; // bigint
let uniqueId = Symbol("id"); // symbol

console.log(typeof name); // "string"
console.log(typeof score); // "number"
console.log(typeof passed); // "boolean"
console.log(nothing); // null
console.log(notSet); // undefined
console.log(typeof bigNumber); // "bigint"
console.log(typeof uniqueId); // "symbol"

What is scope — a beginner-friendly explanation

Scope defines where a variable is visible in the code.

Global scope — accessible anywhere in the script.

Module scope — variables declared at the top level of an ES module are accessible throughout that module but are not automatically available in other modules unless exported.

Function scope — visible only inside the function.

Block scope — visible only inside { … } (for let and const).

Think: each {} is a room. If you declare a variable inside a room, people outside the room can't see it (unless it's global).

Examples:

// global
let globalVar = "I am global";

function example() {
// function scope
var functionVar = "Only in function";
let blockVar = "Only in block";
if (true) {
let insideBlock = "Visible only inside this if-block";
console.log(insideBlock); // works
}
// console.log(insideBlock); // Error: not visible here
}

console.log(globalVar); // visible
// console.log(functionVar); // Error: not visible here (outside function)

Block-scope example showing let/const vs var:

if (true) {
var oldStyle = "var here";
let modern = "let here";
}
console.log(oldStyle); // "var here" — visible outside the block
// console.log(modern); // Error — modern is block-scoped

Module scope example

// math.js
const pi = 3.14159;

export function area(radius) {
return pi * radius * radius;
}

How values can change (and when they can’t)

  • let variables can be reassigned:
  • let age = 20;
    age = age + 1; // now 21
  • const cannot be reassigned — but if it holds an object or array, you can still change the contents:
  • const person = { name: “Alice” };
    person.name = “Bob”; // allowed — the object is mutated
    // person = {}; // not allowed — cannot reassign the variable

Small, clear assignment you can try

Task: Declare variables and print them.

  1. Declare Name, Age, IsStudent using let/const as appropriate.
  2. Print them with console.log.
  3. Try changing the Age (if let) and observe. Try reassigning a const and observe the error.

Sample solution:

const Name = "Madhukar";
let Age = 20;
const IsStudent = true;

console.log(Name, Age, IsStudent); // Madhukar 20 true

Age = 21;
console.log(Age); // 21

// IsStudent = false; // Uncommenting this will raise an error

Originally published by Mr Madhukar

Read the complete article on Medium with full formatting & reader responses.