In JavaScript, primitive data types represent the simplest forms of data. These types are not objects and have no methods. There are seven primitive data types in JavaScript:
-
Undefined: Represents an uninitialized variable or a missing value. A variable that has been declared but not assigned a value is of type
undefined
.let a; console.log(a); // undefined
-
Null: Represents the intentional absence of any object value. It is often used to signify an empty or unknown value.
let a = null; console.log(a); // null
-
Boolean: Represents a logical entity and can have two values:
true
andfalse
.let a = true; let b = false; console.log(a, b); // true false
-
Number: Represents both integer and floating-point numbers. JavaScript uses double-precision 64-bit binary format IEEE 754 values for numbers.
let a = 25; let b = 80.5; console.log(a, b); // 25 80.5
Additionally, there are special numeric values within this type that include
Infinity
,-Infinity
, andNaN
(Not-a-Number). -
BigInt: Introduced in ECMAScript 2020, this type represents integers with arbitrary precision. It can be used for safely storing and operating on large integers even beyond the safe integer limit for Numbers.
let a = 9007199254740991n; let b = a + 1n; console.log(b); // 9007199254740992n
-
String: Represents textual data. It is a set of "elements" of 16-bit unsigned integer values, where each element in the String occupies a position in the sequence. Strings in JavaScript are immutable.
let a = "Hello, world!"; console.log(a); // Hello, world!
-
Symbol: Introduced in ECMAScript 2015, this type represents unique, immutable identifiers. Symbols are often used to add unique property keys to an object that won’t collide with keys any other code might add to the object, and which are hidden from any mechanisms other code will typically use to access the object.
let a = Symbol("description"); let b = Symbol("description"); console.log(a === b); // false
Each of these types serves a specific purpose in JavaScript programming, and understanding their characteristics and behavior is fundamental to effective JavaScript development. Unlike objects, primitives are immutable: when you manipulate a primitive value, what you get back is a new primitive, not a modification of the original value.