Alex Zaharia
Alex Zaharia

Alex Zaharia

Primitives: Introduction to Javascript Data Types - Part 1 🐥

Primitives: Introduction to Javascript Data Types - Part 1 🐥

Taking a Look at Javascript Primitives Data Types

📙 This article is part of the Javascript Deep Dive series.

So, what data types does Javascript have?

The two main categories are: primitives and objects (non-primitives).

In this part we will cover the primitives and in the next one the objects.

Dynamic Types 🌊

Javascript is a dynamically typed language. When you assign a value to a variable you don't specify the type and you can reassign any value to it.

var x = 1; // We assign a number
x = "abc"; // We reassign a string

The type of a variable will be decided at runtime rather than compile time like statically languages such as C++ or Java.

Typeof ğŸ”Ž

You can use the typeof operator to find the type of an element.

> typeof "abc"
> typeof 123
> typeof {x:1}

Primitives ⛰

Primitives in Javascript are types that simply hold a value.

String 🅰

Strings hold textual data. They are basically a list of chars.

In Javascript unlike C++ there is no char type for you to hold only one character. In a string variable you can hold no chars or as many as you want (theoretically the limit is 2^53 - 1 chars which is really big number, unlikely reachable in 99.999% percent of cases)

Strings are represented as 16-bit lists of unsigned integers.

They are indexed from 0.

The length of a String is the number of characters in it. You can access it with .length.

You can use single quotes (' '), double quotes (" ") or back quotes (` `).

var s = ""; // A string with 0 chars
console.log(s.length); // 0

s = "abc"; // A string with 3 chars, "a", "b", "c"
console.log(s.length); // 3

Number 🥇

Numbers hold all kinds of numeric values.

Unlike Java where there are types like int,double, float and more to represent integers and floating point numbers, Javascript uses the number type as an umbrella for all of them.

They are represented as double precision floating point values. Double precision means that they use 64-bits instead of 32-bits the the single floating point precision uses.

More info about double precision can be found on this wiki page: Double-precision floating-point format

The values range between -2^53 - 1 → 2^53 - 1.

To get the maximum value use Number.MAX_VALUE.

> Number.MAX_VALUE

To get the minimum value use Number.MIN_VALUE.

> Number.MIN_VALUE

There are two three symbolic values that have the number type: NaN ,Infinity and -Infinity.


NaN means "Not a Number".

> typeof NaN

It's used to represent unsuccesfull operations with a number or conversions from a non-number type to a number.

> 'a' / 2
> parseInt('a')

Any operation with NaN is a NaN.

> NaN + 1
> NaN * NaN
> NaN - 'abc'
> NaN / false


Infinity is a property available on the global scope that represents the infinity value. It is treated as larger than any other number.

The property is initialized to Number.POSITIVE_INFINITY value.

It's note the same as Number.MAX_VALUE.

> Infinity > 3
> Infinity > Number.MAX_VALUE

If the result of an operation is too large to contain in memory then it will be shown as Infinity.

> Math.pow(2, 10000)

An operation on Infinity results either to Infinity ,-Infinity , 0 or NaN.

> Infinity + 1
> Infinity * -1
> Math.pow(2, Infinity)
> Math.pow(0.3, Infinity)
> Math.pow(1, Infinity)
> 0 * Infinity

Division to zero of a non-negative number gives Infinity.

> 1/0


-Infinity is the negative equivalent of Infinity.

The property is initialized to Number.NEGATIVE_INFINITY value.

It's note the same as Number.MIN_VALUE.

> Infinity < 3
> Infinity < Number.MIN_VALUE

It behaves equivalently to Infinity.

> Math.pow(2, -Infinity)
> -Infinity * 2
> -Infinity + 1
> -Infinity * Infinity
> -Infinity * -Infinity
> Math.pow(0.3, -Infinity)
> Math.pow(1, -Infinity)

BigInt ğŸŽˆ

BigInt can work with values beyond the limits of number. (-2^53 - 1 → 2^53 - 1)

You create a big int by adding n at the end of the number.

> typeof 2n
> 2n ** 53n
> 2n ** 53n + 1n

BigInt works only with BigInt in arithmetic and bitwise operations.

> 3n + 1
Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
> 3n + 3n
> 3n >> 2
Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
> 3n >> 2n

The following arithmetic operators can be used: +, *, -, /, **, and % .

The following bit wise operators can be used: &, |, ~, ^, << and >>.

A BigInt is loosely equal to a number.

> 3n === 3
> 3n == 3
> 3n > 3
> 3n > 2

It works like a number when it's implicitly converted to boolean: if, ||, &&, Boolean, !

Boolean 🤥

TheBoolean type has only two values: true or false.

> typeof true
> typeof false

They are used and resulted in logical operations.

> 3 > 0
> 2 < 1
> "b" > "a"

Undefined 🤔

Undefined is a type that describes declared variables which have not yet been initialized with a value.

> var x
> console.log(x)
> typeof x

Null 📦

Null is a value that can be assigned to a variable to intentionally denote the absence of something.

> var x = null;
> console.log(x)

One quirk to notice here is that the typeof null gives object. This is regarded as a mistake which dates to the first implementations of Javascript.

> typeof null

Symbol ğŸŽ¨

Symbol is a new addition to Javascript. It's a unique, immutable and anonymus value.

> const S = Symbol();
> console.log(S);

You can provide a debug description for the symbol.

> const S = Symbol("a");
> console.log(S);

Two symbols with the same description are not equal.

> const S1 = Symbol("a")
> const S2 = Symbol("a")
> S1 == S2

Every symbol will be unique ⇒ you can use them as keys for objects.

Symbols are not automatically converted to strings.

There is a global registry holding all the symbols. You can use Symbol.for() and Symbol.keyFor() to interact with the registry.

Sources 📚

Reach Out 👋

Was this helpful for you? Let me know what you think in the comments.

You can find me on Twitter and Github

Share this