ResourcesJavascriptGuides

Data Types in Javascript

Introduction

JavaScript supports various data types and data structures. This guide covers all data types in JavaScript, providing examples and explanations for each.


Primitive Data Types

Primitive data types are the most basic data types in JavaScript. They include:

String

Represents textual data

let name = "John";

Number

Represents numeric values

let age = 30;

Bigint

Represents integers with arbitrary precision.

let bigIntValue = 1234567890123456789012345678901234567890n;

Boolean

Represents true or false values

let isActive = true;

Undefined

Represents an uninitialized variable

let notDefined;
let notDefined2 = undefined;

Null

Represents an intentionally absent object value.

let emptyValue = null;

Difference between null and undefined

You can check this article about the difference between null and undefined Null vs undefined

Symbol

Represents a unique identifier.

let sym = Symbol("unique");

Reference Data Types

Reference data types are complex types that reference objects and functions. Also all of them (including date, function, array) is an object.

We have a full guide about reference data types as well the reference as a whole, it is super important topic however neglected by the developers. Reference in Javascript

Object

A collection of key-value pairs.

let person = {
  name: "John",
  age: 30
};

Array

An ordered list of values.

let colors = ["red", "green", "blue"];

Function

A block of code designed to perform a particular task.

function greet() {
  console.log("Hello, World!");
}
 
greet()

Date

Represents date and time

let today = new Date();

Special Data Types in JavaScript

JavaScript also includes special data types and structures:

Typed array

Provides a mechanism for accessing raw binary data

let buffer = new ArrayBuffer(16);
let int32View = new Int32Array(buffer);

Map

A collection of keyed data items, like an object but with key types.

let map = new Map();
map.set("name", "John");

Set

A collection of unique values.

let set = new Set([1, 2, 3]);

WeakMap

A collection of keyed data items where keys are weakly referenced.

let weakMap = new WeakMap();
let obj = {};
weakMap.set(obj, "value");

WeakSet

A collection of objects that are weakly referenced.

let weakSet = new WeakSet();
let obj1 = {};
weakSet.add(obj1);

Javascript and loosely types

JavaScript is a loosely typed language, so you can change the type of a variable after it has been declared.

let variable = "Hello, World!"; // Initially a string
console.log(variable); // Hello, World!
console.log(typeof variable); // string
 
variable = 42; // Now a number
console.log(variable); // 42
console.log(typeof variable); // number
 
variable = true; // Now a boolean
console.log(variable); // true
console.log(typeof variable); // boolean

Resources

MDN Web docs

On this page