Data Types, typeof & Type Coercion
JavaScript data types are divided into two main categories: 7 Primitive Types (stored directly by value) and Reference Objects (stored by reference pointer). In this lesson, you will master all 7 primitives, object literals, the typeof operator and its historic typeof null === "object" quirk, dynamic typing, explicit type conversions vs implicit type coercions, and the 8 Falsy values in JavaScript.
| Data Type | Category | Description / Range | Example |
|---|---|---|---|
| String | Primitive | Textual character data enclosed in '', "", or `...`. | "Ravi" |
| Number | Primitive | 64-bit IEEE 754 floating point (Integers & Decimals up to $2^{53}-1$). | 21, 99.99 |
| BigInt | Primitive | Arbitrary precision large integer ending with n suffix. | 9007199254740991n |
| Boolean | Primitive | Logical truth value: strictly true or false. | true |
| undefined | Primitive | Automatically assigned to declared variables without a value. | let x; // undefined |
| null | Primitive | Intentional absence of any value / empty pointer. | const user = null; |
| Symbol | Primitive | Unique, immutable identifier created via Symbol(). | Symbol("id") |
| Object | Reference | Collection of key-value pairs (Arrays, Functions, Objects). | { name: "Ravi" } |
Oka variable or expression yokka data type ni runtime lo check cheyyadaniki typeof operator vadathamu:
"use strict";
console.log(typeof "Ravi"); // "string"
console.log(typeof 21); // "number"
console.log(typeof 90071992547n); // "bigint"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof Symbol("id")); // "symbol"
console.log(typeof { age: 21 }); // "object"
console.log(typeof function(){}); // "function"
// โ ๏ธ THE HISTORIC JAVASCRIPT BUG:
console.log(typeof null); // "object" (Bug since JS 1.0 in 1995!)
1995 lo original JavaScript implementation lo values memory lo 32-bit units ga store aveyi. Type tags lowest 3 bits lo unte โ 000 was the tag for Object. Null memory address 0x00 (all zeros) gaa read avvanivvadam dwara engine tag check lo 000 (Object) gaa return aindhi. Backward compatibility karanam ga eppatiki dhenini fix cheyyanivvaledhu!
JavaScript is a dynamically-typed language. Variables variables data types ni bind cheskovavu; dynamically values change ayyetappudu type mari pothundi:
let data = 100; // Type: number
data = "Hello World"; // Type: string (Valid in JS!)
data = true; // Type: boolean (Valid in JS!)
Developer manually built-in functions (Number(), String(), Boolean()) dwara type change cheyyadam.
Number("123") // 123
String(456) // "456"
Operators execution appudu JS engine automatically type convert cheyadam.
'5' + 2 // "52" (String concatenation)
'5' - 2 // 3 (Numeric subtraction)
"use strict";
console.log("5" + 10); // "510" (String + Operator converts 10 to string)
console.log("5" - 2); // 3 (Minus operator coerces "5" to number)
console.log("5" * "2"); // 10 (Multiplication coerces strings to numbers)
console.log(true + 1); // 2 (true coerces to 1)
console.log(false + 1); // 1 (false coerces to 0)
JavaScript lo Boolean context lo (like if conditions) evaluate ayye values ni Truthy or Falsy antaru. Exactly 8 Falsy Values unnaayi โ ivevi kaavu anukunte remaining PRAYETHNAM THOO UNNA VALUES ANNI TRUTHY:
"0" (Non-empty string), "false" (Non-empty string), [] (Empty array object), {} (Empty object), function(){} are ALL TRUTHY!
"use strict";
console.log("Boolean(''):", Boolean("")); // false (Falsy)
console.log("Boolean(0):", Boolean(0)); // false (Falsy)
console.log("Boolean(null):", Boolean(null)); // false (Falsy)
console.log("Boolean('0'):", Boolean("0")); // true (Truthy!)
console.log("Boolean([]):", Boolean([])); // true (Truthy!)
console.log("Boolean({}):", Boolean({})); // true (Truthy!)
Run this complete Phase 2 Variables and Data Types code snippet:
"use strict";
const name = "Ravi";
let age = 21;
const isStudent = true;
console.log("Name:", name);
console.log("Age:", age);
console.log("Is Student:", isStudent);
console.log("Type of Age:", typeof age);
// Coercion test
let output = "Age in 5 years: " + (age + 5);
console.log(output);