A quick tour of JavaScript primitives

Dave Mac
Dave Mac
Nov 16, 2016 · 3 min read

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

var a_num = 1.234  // a_num holds the actual value
var an_obj = { type: 'cat' } // an_obj holds a reference to the obj

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
-0         // -0
+0 // 0
0 // alias for +0
-0 === 0 // true!

Number.NEGATIVE_INFINITY // -Infinity
Number.POSITIVE_INFINITY // Infinity
1 / 0 // Infinity
1 / -0 // -Infinity

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.

var key = Symbol()
var an_obj = {
[key]: 'cat'
Symbol() === Symbol() /// false

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

JSON.stringify({ [Symbol('animal')]: cat })   //  {}

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:
var part = 'cat'.substr(0,2)   

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

var part = String.prototype.slice.call('cat', 0, 2)
var partArr = Array.prototype.slice.apply('cat', [0, 2])

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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