Explore JavaScript's 7 primitive types and objects. Learn typeof, prototypal inheritance, built-in objects like Math and Date, and how JSON serialization works.
Why: primitives are immutable values stored by value. JavaScript has 7 primitives: string, number, boolean, null, undefined, bigint, and symbol.
const str = 'hello';
const num = 42;
const float = 3.14;
const bool = true;
const nothing = null;
const notDefined = undefined;
const big = 9007199254740991n; // BigInt
const sym = Symbol('id'); // unique symbol
console.log(typeof str); // 'string'
console.log(typeof num); // 'number'
console.log(typeof bool); // 'boolean'
console.log(typeof nothing); // 'object' ← famous quirk
console.log(typeof notDefined); // 'undefined'
console.log(typeof big); // 'bigint'
console.log(typeof sym); // 'symbol'Why: typeof returns a string describing the type of its operand. Note the quirk: typeof null === "object" — this is a historical bug in JS.
console.log(typeof 42); // 'number'
console.log(typeof 'hello'); // 'string'
console.log(typeof true); // 'boolean'
console.log(typeof undefined); // 'undefined'
console.log(typeof null); // 'object' — quirk!
console.log(typeof {}); // 'object'
console.log(typeof []); // 'object'
console.log(typeof function(){}); // 'function'
console.log(typeof Symbol()); // 'symbol'
console.log(typeof 42n); // 'bigint'
// Safe null check
const val = null;
if (val === null) console.log('it is null');Why: objects are key-value stores. Every object has a prototype — a parent object it inherits methods from. Object.prototype is the root of the chain.
const person = {
name: 'Alice',
age: 30,
greet() {
return `Hi, I'm ${this.name}`;
},
};
console.log(person.name); // 'Alice'
console.log(person.greet()); // "Hi, I'm Alice"
// Prototype chain: person → Object.prototype → null
console.log(person.hasOwnProperty('name'));
console.log(Object.getPrototypeOf(person) === Object.prototype);
// Object methods
console.log(Object.keys(person));
console.log(Object.values(person));
console.log(Object.entries(person));Why: objects can inherit from other objects via the prototype chain. When you access a property, JS walks the chain until it finds it or hits null.
const animal = {
breathe() { return 'breathing'; },
};
const dog = Object.create(animal); // dog's prototype is animal
dog.bark = function() { return 'woof!'; };
console.log(dog.bark()); // 'woof!' — own method
console.log(dog.breathe()); // 'breathing' — inherited
console.log(dog.hasOwnProperty('bark')); // true
console.log(dog.hasOwnProperty('breathe')); // false — inherited
// The chain: dog → animal → Object.prototype → null
console.log(Object.getPrototypeOf(dog) === animal);Why: JS ships with built-in objects like Math, Date, and JSON that provide commonly-needed utilities without imports.
// Math
console.log(Math.max(1, 5, 3)); // 5
console.log(Math.floor(4.9)); // 4
console.log(Math.ceil(4.1)); // 5
console.log(Math.round(4.5)); // 5
console.log(Math.abs(-7)); // 7
// Date
const now = new Date();
console.log(now.getFullYear());
console.log(now.toISOString());
// JSON — serialize and parse
const user = { name: 'Alice', scores: [10, 20, 30] };
const json = JSON.stringify(user);
console.log(json);
const parsed = JSON.parse(json);
console.log(parsed.scores[1]); // 20