Primitives in JavaScript

What Are 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 types denote both integer and floating-point numbers. The range of integer values that can be represented by the Number data type 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

We use cookies and similar technologies to enhance the quality of services, maintain statistics and adjust marketing content. You will find more information in the Cookies Policy.

By clicking OK you grant consent to processing of your personal data by us and our Trusted Partners with the purpose of maintain statistics and adjustment of the marketing content pursuant to the Privacy Policy. If you wish to not grant that consent and/or limit its extent click Settings.