Data types
JavaScript values fall broadly into two categories: "primitive types" and "reference types."
Primitive types (basic types)
There are 7 primitive types in total, and they represent simple values themselves.
-
Number
let num = 42; // Integerlet float = 3.14; // Decimallet infinity = Infinity; // Infinitylet notANumber = NaN; // Not a Number -
String
let str1 = "A double-quoted string";let str2 = 'A single-quoted string';let str3 = `Template literal: ${num}`; // You can embed variables -
Boolean
let isTrue = true;let isFalse = false; -
undefined — a state where no value has been assigned yet
let undefinedVar;console.log(undefinedVar); // undefined -
null — intentionally represents the absence of a value
let emptyValue = null; -
Symbol — a unique value added in ES6
let uniqueKey = Symbol('description'); -
BigInt — a type for handling large integers
let bigNumber = 1234567890123456789012345678901234567890n;
undefined is the state of "no value has been set yet (the system sets it automatically)," while null is the state of "the developer intentionally indicates the absence of a value." Both mean "no value," but they are used in different situations.
Reference types (object types)
Reference types can hold multiple values or more complex data structures.
-
Object
let person = {name: "Tanaka",age: 30,isStudent: false}; -
Array
let fruits = ["Apple", "Banana", "Orange"]; -
Function
function greet() {return "Hello!";}
Primitive types represent simple values themselves, while reference types let you handle multiple values together. There's also this difference: when you copy a primitive type, the value itself is duplicated, but when you copy a reference type, a "reference pointing to the same entity" is shared.
How to check a type
Using the typeof operator, you can check a value's type as a string.
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 (a historical bug; see below)
console.log(typeof {}); // object
console.log(typeof []); // object (an array is also a kind of object)
console.log(typeof function(){}); // function
Checking for null and arrays correctly
Because typeof returns "object" for both null and arrays, you use other methods to check for them.
// Check for null with ===
const value = null;
console.log(value === null); // true
// Check for an array with Array.isArray()
console.log(Array.isArray([1, 2, 3])); // true
console.log(Array.isArray({})); // false
This is a historical bug that originates from JavaScript's early implementation. The internal representation of a value carries a tag indicating its type; the tag for objects was 0, and null was also represented as 0, so it gets judged as an "object." A huge amount of code already depends on this behavior, so it has been left unchanged for backward compatibility. Use value === null to check for null.
What to read next
- Conditionals — Branch your logic using values of each data type