JS Data Types
JS Data Types
JavaScript has primitive data types (simple values) and object types (reference values). Understanding the difference is essential for writing bug-free code.
JS Primitive Data
Primitive types are immutable values: string, number, boolean, undefined, null, symbol, and bigint.
let s = "hello"; // string
let n = 123; // number
let b = true; // boolean
let u; // undefined
let x = null; // null
let sym = Symbol("id"); // symbol
let big = 9007199254740993n; // bigint
console.log(typeof s, typeof n, typeof b);
console.log(typeof u, typeof x); // typeof null is a known JS quirk
console.log(typeof sym, typeof big);
JS Object Types
Object types are reference values. Common object types include: objects, arrays, functions, dates, maps, and sets.
const obj = { name: "Ozan" };
const arr = [1, 2, 3];
const fn = function () { return 1; };
const date = new Date();
console.log(typeof obj); // object
console.log(typeof arr); // object (arrays are objects)
console.log(typeof fn); // function
console.log(date instanceof Date); // true
Because arrays are objects, use Array.isArray() to detect arrays safely.
console.log(Array.isArray([1,2,3])); // true
console.log(Array.isArray({})); // false
JS typeof
typeof returns a string describing the type. It is useful, but it has edge cases.
console.log(typeof "text"); // "string"
console.log(typeof 10); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (quirk)
console.log(typeof [1,2,3]); // "object"
console.log(typeof function(){});// "function"
For more accurate checks (especially for arrays and built-in objects), use Object.prototype.toString.call().
function typeOf(value) {
return Object.prototype.toString.call(value);
}
console.log(typeOf([1,2,3])); // [object Array]
console.log(typeOf(new Date())); // [object Date]
console.log(typeOf(null)); // [object Null]
JS toString()
Many values can be converted to strings using toString(), but be careful: null and undefined do not have toString().
console.log((123).toString()); // "123" console.log(true.toString()); // "true" // console.log(null.toString()); // Error
A safe conversion is String(value).
console.log(String(null)); // "null" console.log(String(undefined)); // "undefined"
JS Type Conversion
JavaScript can convert types automatically (implicit conversion), but explicit conversion is usually safer.
Convert to number:
console.log(Number("42")); // 42
console.log(Number("")); // 0
console.log(Number("abc")); // NaN
console.log(parseInt("42px", 10)); // 42
console.log(parseFloat("3.14")); // 3.14
Tip: Always provide a radix to parseInt() (usually 10).
Convert to boolean:
console.log(Boolean(0)); // false
console.log(Boolean("")); // false
console.log(Boolean(null)); // false
console.log(Boolean(undefined));// false
console.log(Boolean("0")); // true
console.log(Boolean("false")); // true (important!)
Convert to string:
console.log(String(123)); // "123" console.log(String(true)); // "true"
Primitive vs Reference (Modern Must-Know)
Primitives are copied by value. Objects are copied by reference.
let a = 10;
let b = a;
b = 20;
console.log(a, b); // 10 20
const o1 = { x: 1 };
const o2 = o1;
o2.x = 99;
console.log(o1.x, o2.x); // 99 99
To copy objects safely, modern browsers support structuredClone() for many data types.
const original = { a: 1, nested: { b: 2 } };
const copy = structuredClone(original);
copy.nested.b = 999;
console.log(original.nested.b); // 2
Note: JSON.parse(JSON.stringify(obj)) is a common trick but it loses dates, undefined, functions, and more.
Next Step
Continue with JS Errors to learn how JavaScript reports problems and how to handle them safely.