JavaScript has 8 data types:
Primitive types (7):
number — numbersstring — stringsboolean — logical typeundefined — undefined valuenull — empty valuesymbol — unique identifier (ES6)bigint — large integers (ES2020)Reference type (1):
object — objects (including arrays, functions, dates, etc.)Includes integers, decimals, special values.
let age = 25; // integer
let price = 99.99; // decimal
let infinity = Infinity; // infinity
let notANumber = NaN; // "not a number"
console.log(typeof age); // "number"
console.log(typeof infinity); // "number"
console.log(typeof notANumber); // "number"Features:
Number.MAX_SAFE_INTEGER (2^53 - 1)NaN is not equal to itself: NaN === NaN → falseNumber.isNaN(), Number.isFinite()Sequence of characters in quotes.
let name = "Aleksandr"; // double quotes
let surname = 'Ermolov'; // single quotes
let fullName = `${name} ${surname}`; // template strings (ES6)
console.log(typeof name); // "string"
console.log(fullName); // "Aleksandr Ermolov"Features:
name[0] or name.charAt(0)name.lengthOnly two values: true or false.
let isActive = true;
let isCompleted = false;
let isAdult = age >= 18; // comparison result
console.log(typeof isActive); // "boolean"Boolean conversion:
// Falsy values (convert to false):
Boolean(0); // false
Boolean(""); // false
Boolean(null); // false
Boolean(undefined); // false
Boolean(NaN); // false
Boolean(false); // false
// Everything else is truthy (converts to true)
Boolean(1); // true
Boolean("hello"); // true
Boolean([]); // true
Boolean({}); // trueValue of a variable that is declared but not initialized.
let x;
console.log(x); // undefined
console.log(typeof x); // "undefined"
// Function without return also returns undefined
function test() {
// no return
}
console.log(test()); // undefinedRepresents intentional absence of value.
let data = null;
console.log(data); // null
console.log(typeof data); // "object" (this is a bug in JS!)
// Correct null check
console.log(data === null); // trueDifference between null and undefined:
let a; // undefined (not initialized)
let b = null; // null (intentionally empty)
console.log(a == b); // true (loose comparison)
console.log(a === b); // false (strict comparison)Unique identifier introduced in ES6.
let id1 = Symbol("id");
let id2 = Symbol("id");
console.log(id1 === id2); // false (always unique)
console.log(typeof id1); // "symbol"
// Using in objects
let user = {
name: "Aleksandr",
[id1]: 123 // symbol as key
};
console.log(user[id1]); // 123For working with numbers larger than Number.MAX_SAFE_INTEGER.
let bigNumber = 123456789012345678901234567890n; // suffix 'n'
let anotherBig = BigInt("123456789012345678901234567890");
console.log(typeof bigNumber); // "bigint"
// Cannot mix with regular numbers
// console.log(bigNumber + 1); // TypeError!
console.log(bigNumber + 1n); // worksEverything that is not a primitive is an object.
// Regular object
let person = {
name: "Aleksandr",
age: 30
};
// Array (also an object!)
let numbers = [1, 2, 3, 4, 5];
// Function (also an object!)
function greet() {
return "Hello!";
}
// Date (object)
let now = new Date();
console.log(typeof person); // "object"
console.log(typeof numbers); // "object"
console.log(typeof greet); // "function" (special case)
console.log(typeof now); // "object"Difference between primitives and objects:
// Primitives are passed by value
let a = 5;
let b = a;
a = 10;
console.log(b); // 5 (unchanged)
// Objects are passed by reference
let obj1 = { x: 5 };
let obj2 = obj1;
obj1.x = 10;
console.log(obj2.x); // 10 (changed!)| Data Type | typeof | Check Example |
|---|---|---|
| number | ”number” | typeof 42 |
| string | ”string” | typeof "hello" |
| boolean | ”boolean” | typeof true |
| undefined | ”undefined” | typeof undefined |
| symbol | ”symbol” | typeof Symbol() |
| bigint | ”bigint” | typeof 123n |
| function | ”function” | typeof function(){} |
| object | ”object” | typeof {}, typeof [] |
| null | ”object” | value === null |
More precise checking methods:
// Array check
Array.isArray([1, 2, 3]); // true
Array.isArray({}); // false
// Null check
value === null; // true for null
// NaN check
Number.isNaN(NaN); // true
isNaN("hello"); // true (but imprecise!)
// Universal type check
Object.prototype.toString.call([]); // "[object Array]"
Object.prototype.toString.call({}); // "[object Object]"console.log(typeof null);console.log(typeof NaN);let a = [];
let b = {};
let c = function() {};
console.log(typeof a, typeof b, typeof c);console.log(0 == false);
console.log("" == false);
console.log(null == undefined);let x = Symbol("test");
let y = Symbol("test");
console.log(x === y);let big = 9007199254740991n;
console.log(typeof big);
console.log(big + 1);// Bad
if (value == null) { /* ... */ }
// Good
if (value === null || value === undefined) { /* ... */ }
// or
if (value == null) { /* only for null/undefined check */ }// For arrays
if (Array.isArray(value)) { /* ... */ }
// For null
if (value === null) { /* ... */ }
// For objects (excluding null and arrays)
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
/* ... */
}// Unexpected results
console.log("5" + 3); // "53" (concatenation)
console.log("5" - 3); // 2 (subtraction)
console.log(+"5"); // 5 (unary plus)
console.log(!!"hello"); // true (double negation)typeof null === "object" is a bug, use === nullKnowing data types is the foundation of JavaScript, without which it’s impossible to write quality code!
Want more articles on interview preparation?
Follow EasyAdvice, bookmark the site, and level up every day 💪