JavaScript contains 9 fundamental data types:
Number
The Number
data type represents numbers in JavaScript. It can be used for both integers and floating-point numbers. The value NaN
is also considered of the Number
type.
let age = 20; console.info(typeof age) // number
String
The String
data type represents sequences of text characters. Text is enclosed in single or double quotes.
let name = "Bob"; console.info(typeof name) // string
Boolean
The Boolean
data type can have one of two values: true
(truth) or false
(falsehood). It is used to represent logical states.
let isMale = true; console.info(typeof isMale) // boolean
Null
The Null
data type represents the absence of a value or emptiness. It is a special value that signifies a lack of data.
Pay attention to typeof
result!
let data = null; console.info(typeof data) // object
Undefined
The Undefined
data type represents a variable that has been declared but hasn't been assigned a value. It's also the default value of variables.
let data; console.info(data); // undefined console.info(typeof data) // undefined
Object
The Object
data type represents complex objects that can contain various properties and methods. Objects are fundamental data structures in JavaScript.
let person = { name: "Bob", age: 20, }; console.info(typeof person) // object
Function
The Function
data type represents functions in JavaScript. Functions are blocks of code that can be called and used to perform specific actions.
function add(a, b) { return a + b; } console.info(typeof add) // function
Symbol (from ES6)
The Symbol
data type was introduced in ECMAScript 6 (ES6). It's a unique and immutable type used to create unique property keys in objects.
const mySymbol1 = Symbol("symbol"); const mySymbol2 = Symbol("symbol"); const obj = {}; obj[mySymbol1] = "abc"; obj[mySymbol2] = "cba"; console.info(obj[mySymbol1]) // abc console.info(obj[mySymbol2]) // cba console.info(typeof mySymbol1) // symbol
BigInt (from ES11)
The BigInt
data type was introduced in ECMAScript 11 (ES11). It's a special data type used to represent arbitrarily large numbers with no numerical limits. It's denoted by adding n
or N
at the end of a number.
const bigNumber = 1234567890123456789012345678901234567890n; console.info(typeof mySymbol1) // bigint