A quick tour of JavaScript primitives

Primitives are the simplest elements of a programming language. JavaScript currently has six primitive types: string, number, boolean, null, undefined, symbol , everything else is an object — yes, including arrays and dates.

All the types are used to define immutable values — values which can not be changed. We call values of these types primitive values.

A primitive type has a fixed size in memory. For example, a number occupies eight bytes of memory, and a boolean value can be represented with only one bit. The number type is the largest of the primitive types.

Variables hold the actual values of primitive types, but they hold only references to the values of reference types

Boolean type

  • Allowed values: true,false.

Null type

  • Allowed values: null.

It is worth noting that the typeof operator returns object for null because the spec says so.

Undefined type

  • Allowed values: undefined.

The Undefined type has exactly one value, called undefined. Any variable that has not been assigned a value has the value undefined.

Number type

  • Size: 8 bytes
  • Allowed values: 18437736874454810624 finite numbers (half positive half negative, including positive and negative zero) and three symbolic values NaN, Positive Infinity & Negative Infinity
  • Standard: Double-precision 64-bit binary format IEEE 754 value

To check for larger or smaller values than +/-Infinity, you can use the constants Number.MAX_VALUE or Number.MIN_VALUE . ES6 allows you to check if a number is in the double-precision floating-point number range using Number.isSafeInteger() as well as Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER.

String type

  • Size: variable (string.length * 2 bytes)
  • Max size: 18014398509481982 bytes (~16gB)
  • Allowed values: 16-bit unsigned integer values (0 to 65535 inclusive)
  • Encoding: UTF-16

JavaScript’s string type is used to represent textual data. It is a set of "elements" of 16-bit unsigned integer values each treated as a UTF-16 code value.

JavaScript strings are immutable (unchangeable), once a string is created it is impossible to modify it. However, it is still possible to create another string based on the original by using string functions such as substr and concat.

An empty string has length zero and therefore contains no elements.

Symbol type

Symbols are new to ES6 they are unique and immutable primitive values and may be used as the key of an Object property.

Symbols are not enumerable in for…in iterations and will not serialize with JSON.stringify()

Auto-boxing of primitive types

When ‘auto-boxing’ JavaScript will automatically convert the primitive value to an object, it happens in two situations:

  1. When accessing a property on a primitive, such as in the following example:

2. When a primitive is passed as the this argument to .call or .apply

--

--

Web Development Tidbits

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store