# Primitive Type
built-in data structures. not an object (opens new window) and has no methods (opens new window).
There are 7 primitive data types: string (opens new window), number (opens new window), bigint (opens new window), boolean (opens new window), undefined (opens new window), symbol (opens new window), and null (opens new window).
primitives are immutable
A primitive can be replaced, but it can't be directly altered.
It is important not to confuse a primitive itself with a variable assigned a primitive value. The variable may be reassigned a new value, but the existing value can not be changed in the ways that objects, arrays, and functions can be altered.
// Using a string method doesn't mutate the string
var bar = "baz";
console.log(bar); // baz
bar.toUpperCase();
console.log(bar); // baz
const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.toUpperCase());
// expected output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."
//The toUpperCase() method returns the value of the string converted to uppercase. This method does not affect the value of the string itself since JavaScript strings are immutable.
// Using an array method mutates the array
var foo = [];
console.log(foo); // []
foo.push("plugh");
console.log(foo); // ["plugh"]
// Assignment gives the primitive a new (not a mutated) value
bar = bar.toUpperCase(); // BAZ
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types:
Six Data Types that are primitives (opens new window), checked by typeof (opens new window) operator:
- undefined (opens new window) :
typeof instance === "**undefined**"
- Boolean (opens new window) :
typeof instance === "boolean"
- Number (opens new window) :
typeof instance === "number"
- String (opens new window) :
typeof instance === "string"
- BigInt (opens new window) :
typeof instance === "bigint"
- Symbol (opens new window) :
typeof instance === "symbol"
# JS Data and Structure Types
- Six Datatypes that are primitives, checked by typeof operator:
- undefined (opens new window) :
typeof instance === "**undefined**"
- Boolean (opens new window) :
typeof instance === "boolean"
- Number (opens new window) :
typeof instance === "number"
- String (opens new window) :
typeof instance === "string"
- BigInt (opens new window) :
typeof instance === "bigint"
- Symbol (opens new window) :
typeof instance === "symbol"
- undefined (opens new window) :
- Structural Types (typeof will always receive object):
- Object (opens new window) :
typeof instance === "**object**"
. Special non-data but Structural type for any constructed (opens new window) object instance also used as data structures: newObject
(opens new window), newArray
(opens new window), newMap
(opens new window), newSet
(opens new window), newWeakMap
(opens new window), newWeakSet
(opens new window), newDate
(opens new window) and almost everything made with new keyword (opens new window); - Function (opens new window) : a non-data structure, though it also answers for
typeof
operator:typeof instance === "**function**"
. This is merely a special shorthand for Functions, though every Function constructor is derived from Object constructor.
- Object (opens new window) :
- Structural Root Primitive:
- null (opens new window) :
typeof instance === "**object**"
. Special primitive (opens new window) type having additional usage for its value: if object is not inherited, thennull
is shown;
- null (opens new window) :