# 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
1
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:

# JS Data and Structure Types

# Wrapper

Last Updated: 3/1/2021, 9:19:08 PM