Variables & Types

JS Fundamentals · free study notes · Updated

JavaScript Variables & Types

Coding & Web Development – JavaScript


1. Simple Explanation

In JavaScript, variables are containers that hold data. Unlike some other languages, JavaScript is dynamically typed: you can change the type of data stored in a variable at any time, and the language will figure it out for you.

Types describe the kind of value a variable can hold. JavaScript has a handful of primitive types (string, number, boolean, null, undefined, symbol, bigint) and non‑primitive types (object, array, function, date, etc.).


2. Key Points

Concept What It Means Why It Matters
var Function‑scoped, hoisted, can be redeclared Legacy; avoid in modern code
let Block‑scoped, no redeclaration, no hoisting (temporal dead zone) Preferred for most variables
const Block‑scoped, cannot be reassigned Use for values that should stay constant
Primitive Types string, number, bigint, boolean, undefined, null, symbol Immutable; simple comparisons
Non‑Primitive Types object, array, function, date, etc. Mutable; compare by reference
Dynamic Typing Variable can hold any type at any time Flexibility, but can lead to bugs
Type Coercion Automatic conversion during operations (e.g., "5" + 1 === "51") Useful but can cause surprises
typeof Operator that returns a string describing a variable’s type Helps with runtime type checks
instanceof / Array.isArray() Check for specific object types Important for complex data structures
Best Practice Prefer let/const; avoid var; keep types clear; use === for comparison Keeps code predictable & maintainable

3. Types Overview

Primitive Type Example Typical Use Notes
string "Hello, world!" Text Can use single, double, or backticks (template literals)
number 42, 3.14 Numeric values All numbers are IEEE‑754 floating point
bigint 9007199254740991n Whole numbers beyond 2^53‑1 Requires n suffix
boolean true, false Flags Only two values
undefined undefined Uninitialized Returned when variable exists but no value is assigned
null null No value Explicit “empty” value
symbol Symbol("id") Unique identifiers Useful for property keys
Non‑primitive
object { name: "Alice" } Structured data Mutable
array [1, 2, 3] Ordered list Subclass of object
function function foo() {} Code blocks First‑class objects
date new Date() Time Built‑in object
regexp /abc/ Regular expressions Pattern matching

4. Declaration Keywords

// var
var x = 10;      // function scoped, hoisted

// let
let y = 20;      // block scoped, no redeclaration

// const
const PI = 3.14159; // block scoped, cannot be reassigned

When to use each

Situation Recommended Keyword
Constant values that never change const
Variables that may be reassigned within a block let
Legacy code or global functions (rare) var

5. Worked Example

Below is a step‑by‑step example that demonstrates variable declaration, type changes, type checking, and coercion.

// 1. Declaration
let userInput = "25";          // string
const MAX_AGE = 120;           // number (constant)

// 2. Type checking
console.log(typeof userInput);   // "string"
console.log(typeof MAX_AGE);     // "number"

// 3. Conversion (explicit coercion)
const ageNumber = Number(userInput); // 25 (number)
const ageBigInt = BigInt(userInput); // 25n (bigint)

// 4. Comparison
console.log(ageNumber === MAX_AGE);   // true
console.log(ageNumber == MAX_AGE);    // true (loose equality)

// 5. Arithmetic with coercion
let result = userInput + 5;          // "255" (string concatenation)
console.log(result);                 // "255"

result = Number(userInput) + 5;      // 30 (number addition)
console.log(result);                 // 30

// 6. Using objects
const user = {
  name: "Alice",
  age: ageNumber
};

console.log(user.name);   // "Alice"
console.log(user.age);    // 25

// 7. Array example
let scores = [10, 20, 30];
scores.push(40);          // modify array

console.log(scores.length);   // 4
console.log(scores[2]);      // 30

Explanation of Key Steps

  1. Declaration & Initial Value – userInput starts as a string because it comes from an input field. 2

Your note

Page 1

Doubt hai is topic me?

Free AI teacher se poocho — 24/7, step by step.