Data Types, typeof & Type Coercion

๐ŸŸจ JavaScript (ES2026+) ๐ŸŸข Lesson 4 ๐Ÿ“‚ Phase 02: Variables & Data Types ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this lesson: 7 Primitives (String, Number, BigInt, Boolean, undefined, null, Symbol) ยท Objects ยท typeof Operator ยท Historic typeof null Bug ยท Dynamic Typing ยท Explicit Conversion ยท Implicit Coercion ยท Truthy & Falsy Values

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.

1The 7 Primitive Data Types + Objects
Data TypeCategoryDescription / RangeExample
StringPrimitiveTextual character data enclosed in '', "", or `...`."Ravi"
NumberPrimitive64-bit IEEE 754 floating point (Integers & Decimals up to $2^{53}-1$).21, 99.99
BigIntPrimitiveArbitrary precision large integer ending with n suffix.9007199254740991n
BooleanPrimitiveLogical truth value: strictly true or false.true
undefinedPrimitiveAutomatically assigned to declared variables without a value.let x; // undefined
nullPrimitiveIntentional absence of any value / empty pointer.const user = null;
SymbolPrimitiveUnique, immutable identifier created via Symbol().Symbol("id")
ObjectReferenceCollection of key-value pairs (Arrays, Functions, Objects).{ name: "Ravi" }
2The typeof Operator & Historic "typeof null" Quirk

Oka variable or expression yokka data type ni runtime lo check cheyyadaniki typeof operator vadathamu:

JavaScript โ€” typeof Demo โ–ถ Run Code
"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!)
๐Ÿ’ก Why does 'typeof null' return "object"?

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!

3Dynamic Typing in JavaScript

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!)
4Type Conversion (Explicit) vs Type Coercion (Implicit)
1. Explicit Type Conversion

Developer manually built-in functions (Number(), String(), Boolean()) dwara type change cheyyadam.

Number("123") // 123
String(456) // "456"
2. Implicit Type Coercion

Operators execution appudu JS engine automatically type convert cheyadam.

'5' + 2 // "52" (String concatenation)
'5' - 2 // 3 (Numeric subtraction)
JavaScript โ€” Coercion Examples โ–ถ Run Code
"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)
5Truthy vs Falsy Values in JavaScript

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:

๐Ÿšซ THE EXACT 8 FALSY VALUES IN JAVASCRIPT: 1. false 2. 0 (Integer zero) 3. -0 (Negative zero) 4. 0n (BigInt zero) 5. "" or '' or `` (Empty String) 6. null 7. undefined 8. NaN (Not a Number)
โœ… Notable Truthy Values (Common Traps):

"0" (Non-empty string), "false" (Non-empty string), [] (Empty array object), {} (Empty object), function(){} are ALL TRUTHY!

JavaScript โ€” Truthy & Falsy Test โ–ถ Run Code
"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!)
๐Ÿ’ป Try It Yourself โ€” Curriculum Code Challenge

Run this complete Phase 2 Variables and Data Types code snippet:

JavaScript โ–ถ Run Code
"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);
Run in Our Compiler โ†’
OC
Written and reviewed by Our Compiler Technical Team ยท Updated for JavaScript ES2026+