The Seven Standard Primitives in JavaScript

Apr 29, 2021

Javascript has seven primitive data types:

In JavaScript, a primitive is any value that isn't an object.

The typeof operator tells you what type a primitive value is.

String

It is important to note that strings can be either a primitive, string literal, or an object. Javascript automatically converts string primitives to objects to enable the user to use the handy array of functions available for use. If you wanted to convert a string object to a primitive, you would use the valueOf() method.

let message = "Hello World";
typeof message; // 'string'

Number

In other programming languages, you can have numbers be defined as floats, integers, doubles and so on and so forth. Javascript simplified this feature by just making every number it comes across Number, with an exception.

let password = 123456789;
typeof password; // 'number'

BigInt

This is similar to Number however, it allows you to safely represent integer values larger than 253. You can create a bigint by appending n to the end of the number or by wrapping the number, or string for this case, in the BigInt() constructor.

let launchCodes = 66777888889999912345n;
typeof launchCodes; // 'bigint'

Boolean

This data type is what contains the values true or false. This is great in that you can use other values to represent that same concept like 0 for false, and 1 for true.

isBool = true;
typeof isBool; // 'boolean'

// Instances of wrapper classes, like `Boolean`, are objects, **not** primitives.
typeof new Boolean(isBool); // 'object'

Undefined

This value is automatically assigned to variables that have just been declared but not defined. If you were to put this in an if statement, the value would be false so this is another handy thing you could use like mentioned in the Boolean section.

let x;

typeof x; // 'undefined'

Symbol

A Symbol is a value created by invoking the Symbol function which is guaranteed to create a unique value. It takes one parameter, a string description, that will show up when you print the symbol.

let x = Symbol("this is a symbol");
typeof x; // 'symbol'

Null

null is special because the typeof operator reports its type as 'object'.

typeof null; // 'object'

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Fundamentals Tutorials