JavaScript is **dynamically typed**: variables can hold any type and types can change at runtime. There are 7 **primitive** types (immutable) and 1 **reference** type (Object, including Arrays and Functions).
1Understanding Data Types
JavaScript is dynamically typed: variables can hold any type and types can change at runtime. There are 7 primitive types (immutable) and 1 reference type (Object, including Arrays and Functions).
typeof null === 'object' is a legacy bug from JS v1. Use === null to check for null.
// Primitive types
let str = 'hello'; // string
let num = 42; // number
let big = 9007199n; // bigint
let bool = true; // boolean
let undef; // undefined
let nul = null; // null
console.log(typeof str, typeof num, typeof bool);2Practical Example
Here is a real-world application of Data Types showing how it is used in production JavaScript code.
// Type coercion pitfall
console.log('5' + 3); // '53' (string concat)
console.log('5' - 3); // 2 (numeric)
console.log('' == false); // true (loose equality)
console.log('' === false); // false (strict)3Best Practices
Follow these guidelines when working with Data Types:
1. Use typeof to check primitives
2. Use Array.isArray() for arrays
3. Prefer strict equality (===) to avoid type coercion
Tip: typeof null === 'object' is a legacy bug from JS v1. Use === null to check for null.
// Primitive types
let str = 'hello'; // string
let num = 42; // number
let big = 9007199n; // bigint
let bool = true; // boolean
let undef; // undefined
let nul = null; // null
console.log(typeof str, typeof num, typeof bool);