DEV Community

Cover image for The Quite Nice and Fairly Accurate Intro to JS Primitive Data Types (pt. 1)
Alexandra
Alexandra

Posted on • Updated on

The Quite Nice and Fairly Accurate Intro to JS Primitive Data Types (pt. 1)

JavaScript has two data types: Primitives, and objects. A primitive (or a primitive value or a primitive data type), as described in the JavaScript documentation, is the data that is not an object and has no methods.

JavaScript is a dynamically typed language which means that even though there are data types in its ecosystem the variables are not bound to any of them.

There are 6 primitive data types in JS: Boolean, Number, String, Null, Undefined, Symbol (ES6)

Boolean

Boolean is a logical data type which can only have two values: true or false; It is a YES-NO switch; Logical operation results in a boolean value;

Common use case: control application flow

boolean-1

Number

Number is a numeric data type in the double-precision 64-bit floating point format. In JS, number represents both integers and floating points.
A number can also be +Infinity, -Infinity, and NaN (Not A Number).

Common use case: mathematical calculations

number

String

A string is a sequence of characters used to represent text. In JS, a string is inside of double or single quotes. ES6 also introduced template literals or template strings. Template literals are string literals allowing embedded expressions (${}). The expression inside ${…} is evaluated and the result becomes a part of the string.

Common use case: Store text

string

Null

In programming, null usually represents a reference that points to a nonexistent/invalid object or address in memory. In JS though is a special primitive type which represents "nothing".

null

Note: The typeof null is 'object'. It is considered as a bug (why null is object)

Undefined

Undefined is a data type that it stands for a value that is not defined;

Common use case: Check if a variable is assigned with a value

undef

Undefined vs Null

Symbol

Symbols are introduced in ES6 and are completely unique identifiers. Just like the other primitives they can be created using the factory function Symbol() which returns a Symbol (i.e. new Boolean('true') creates a new boolean value).

Common use case: Create keys, Privacy, Protocols

symbol-1

References:

A quick overview of JavaScript symbols

JavaScript Documentation

JavaScript Data Types Explained

Top comments (0)