Data Types

Anonymous contributor's avatar
Anonymous contributor
Published May 6, 2021Updated May 15, 2024
Contribute to Docs

Data types are used to classify eight fundamental values used for programming in JavaScript, including primitive and object types.

Primitive Data Types

JavaScript uses seven primitive data types that are listed in the table below:

Data Type Wrapper Object Description
number Number Any number, including numbers with decimals: 1, -2, 99, 3.14.
bigint BigInt Any number, greater than 253-1 or less than -(253-1) with n appended to the number: 1234567890123456n.
string String Any grouping of characters on your keyboard (letters, numbers, spaces, symbols, etc.) surrounded by single '' or double "".
boolean Boolean This data type only has two possible values — either true or false.
null None This data type represents the intentional absence of a value, and is represented by the keyword null.
undefined None This data type is denoted by the keyword undefined. It also represents the absence of a value though it has a different use than null.
symbol Symbol A newer feature to the language, symbols are unique identifiers, useful in more complex coding. No need to worry about these for now.

Objects

Objects are a little more complex since they are collections of related data.

Note: The null type cannot be tested with typeof like all the other primitive types because it will return "object". This must be tested with the type equality operator (i.e., === null).

Number and BigInt

In JavaScript, numbers are always stored as double-precision floating point numbers.

let num = 7;
let bigNum = 9999999999999999n;

BigInt is necessary for large whole numbers because they are unreliable with the Number type:

console.log(9999999999999999);
console.log(9999999999999999n);

This will output the following:

10000000000000000
9999999999999999n

The integer precision for the Number type is 15 digits. The following example is runnable:

Code
Output
Loading...

String

Strings in JavaScript can be defined with either ' ' or " ".

let greeting = 'Hi buddy';
let message = 'You are doing great! Keep studying!';

To find the length of the string, the built-in length property is used. Run the following example to get a better idea:

Code
Output
Loading...

Boolean

Boolean is for truthy or falsy values:

let lateToWork = true;
let isTheEarthFlat = false;

To convert a non-boolean value into a boolean, the Boolean function or double not !! is used. Run the following code block to see how Boolean() works:

Code
Output
Loading...

Null and Undefined

Null and Undefined are both for the absence of a value, but they have different meanings.

// Undefined means there should be some values, but it is undefined now
let finishCourseTime = undefined;
// Null means there is no value here
let finishStudyingDate = null;

A function also outputs undefined when no value is returned.

Code
Output
Loading...

The same function outputs null when null is returned.

Code
Output
Loading...

Object

Objects can have fields with different values and types inside:

const user = {
name: 'Jane',
age: 20,
isActive: true,
};

Alternatively, an empty object can be initialized and the properties added to the object. Run the code below to see it in action:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn JavaScript on Codecademy