Primitives
6 + 1 Primitives in JavaScript?
In ES6 there are six primitive data types: undefined
, boolean
, number
, string
, bigInt
, and symbol
.
Further, a special primitive type is null
, aka Structural Root Primitive.
Immutability
All primitive type values in JavaScript are immutable.
Checking Type
To check whether a given value is of a given type typeof
is used.
typeof undefined === 'undefined' // => true
typeof true === 'boolean' // => true
typeof 1 === 'number' // => true
typeof '1' === 'string' // true
typeof BigInt(4242424242424242424242424242424) === 'bigint' // => true
typeof Symbol() === 'symbol' // => true
typeof null === 'object' // true
undefined
undefined
is used to express that the referenced value does not exist. For example, a variable that has been declared but has not been assigned any value has the value of undefined
assigned automatically.
boolean
There are two Boolean values in JavaScript: true
and false
.
number
The number
data type denotes both an integer or a floating-point number.
The range of integer values that the number
data type can represent is from -(2^53 − 1) to 2^53 − 1. To check whether a given integer number can be safely expressed by the number
data type the Number.isSafeInteger()
method can be used. If a given integer falls out of the range it is expressed by double-precision floating-point approximation. This problem is solved by the bigint
data typed.
bigint
The BigInt data type can be used to represent integers with arbitrary precision that fall out of the save range of the Number data type. A BigInt is created through appending n
at the end of a given integer of Number type or through use of the BigInt() constructor.
typeof 9007199254740992n // => "bigint"
typeof BigInt(9007199254740992) // => "bigint"
string
JavaScript type String is dedicated to representing textual data. A string in JavaScript is an ordered set of characters which in turn are visual representations of 16-bit unsigned integers. The index of the first character is 0
.
Strings in JavaScript are immutable.
// String.substr() is used to get a substring.
'abcdef'.substr(1,3) // => "bcd"
//To concatenate a string use String.concat().
'abc'.concat('def') // => "abcdef"
symbol
A primitive type Symbol is a novelty introduced in ES6 to JavaScript. A symbol in JavaScript is an anonymous, unique value, an identifier that can be used as an object property.
To create a symbol use the function Symbol()
to which a description argument can be provided. Providing the same description twice to Symbol()
does not make the two symbols equal. Each symbol in JavaScript is unique.
Symbol('foo') === Symbol('foo') // => false
const symA = Symbol()
const symB = Symbol()
const anObj = {
[symA]: 1,
[symB]: 2
}
console.log(anObj) // {Symbol(): 1, Symbol(): 2}
anObj[symA] // => 1
anObj[symB] // => 2
Symbol properties are ignored by JSON.stringify()
.
null
null
is used to denote that the referenced value exists but the value is used to express emptiness. null
is often used as value to keys where an object value can be expected in the future.
foo // Uncaught ReferenceError: foo is not defined
let foo // => undefined
foo = null // => null