Yandex

JavaScript Data Types
Primitive, Reference, Dynamic Typing



Introduction to JavaScript Data Types

Every value in JavaScript belongs to a data type. Understanding these data types is essential to control behavior, write meaningful conditions, and build flexible functions. JavaScript's type system includes both primitive types and reference types.

1. Primitive Data Types

Primitive types are immutable and directly represent a single value. JavaScript provides six primitive types:

  • Number
  • String
  • Boolean
  • Undefined
  • Null
  • Symbol (introduced in ES6)
  • BigInt (introduced in ES11/ES2020)

Number


let score = 99.5;
let count = 10;
typeof score => 'number'
typeof count => 'number'

JavaScript uses a single type for both integers and floating-point numbers.

String


let name = "Alice";
let greeting = 'Hello';
let message = `Welcome, ${name}`;
typeof name => 'string'
typeof message => 'string'

Boolean


let isLoggedIn = true;
let isAdmin = false;
typeof isLoggedIn => 'boolean'

Undefined


let user;
console.log(user);
undefined

Any variable declared but not initialized is undefined.

Null


let session = null;
typeof session => 'object' // This is a known JavaScript quirk

Symbol (ES6)


let sym = Symbol('id');
typeof sym => 'symbol'

Symbols are unique and immutable, often used for unique keys in objects.

BigInt (ES2020)


let bigNumber = 9007199254740991n;
typeof bigNumber => 'bigint'

2. Reference Data Types

Unlike primitive values, reference types store memory addresses, not actual values. These include:

  • Objects
  • Arrays
  • Functions

Object


let user = {
  name: "Alice",
  age: 25
};
typeof user => 'object'

Array


let colors = ["red", "green", "blue"];
typeof colors => 'object'
Array.isArray(colors) => true

Function


function greet() {
  return "Hi there!";
}
typeof greet => 'function'

3. JavaScript is Dynamically Typed

JavaScript doesn’t require variable types to be declared explicitly. Types are assigned at runtime:


let dynamic = 5;         // number
dynamic = "now a string"; // string
dynamic = true;          // boolean
typeof dynamic => changes at runtime

4. Type Checking with typeof

The typeof operator helps identify the data type of a variable:


console.log(typeof 42);           // 'number'
console.log(typeof 'hello');     // 'string'
console.log(typeof null);        // 'object' (legacy bug)
console.log(typeof undefined);   // 'undefined'
console.log(typeof Symbol());    // 'symbol'

5. Comparing null and undefined

While both represent empty values, their meanings differ:


let a;          // undefined
let b = null;   // null

console.log(a == b);  // true (type coercion)
console.log(a === b); // false (strict comparison)

6. Special Case: NaN

NaN stands for “Not a Number”. It is returned when a mathematical operation fails:


let invalid = "abc" / 2;
console.log(invalid);       // NaN
console.log(typeof invalid); // 'number'

7. Wrapper Objects (Boxing)

Primitive values can temporarily behave like objects. JavaScript automatically wraps them in object wrappers:


let str = "hello";
console.log(str.toUpperCase()); // "HELLO"

Behind the scenes, JavaScript uses new String(str) to allow method calls.

8. Type Conversion (Coercion)

Implicit Conversion


console.log("5" + 1);   // "51"
console.log("5" - 1);   // 4
console.log(true + 1);  // 2

Explicit Conversion


console.log(Number("5"));      // 5
console.log(String(100));      // "100"
console.log(Boolean(0));       // false

Summary Table: Data Types Overview

Type Category Example
Number Primitive 42
String Primitive "JavaScript"
Boolean Primitive true
Null Primitive null
Undefined Primitive let a;
Symbol Primitive Symbol("id")
BigInt Primitive 12345678901234567890n
Object Reference { name: "Alice" }
Array Reference [1, 2, 3]
Function Reference function() {}

Conclusion

JavaScript's data types form the foundation of every program. By understanding both primitive and reference types, dynamic typing, and the quirks of type coercion, you gain the power to write cleaner, more predictable code. This knowledge also lays the groundwork for mastering scopes, closures, object-oriented JS, and advanced data structures.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M