JS Data Types

JavaScript has two main categories of data types: Primitive and Non-Primitive (Reference types).


Primitive Data Types:

Primitive data types are immutable and stored directly in memory.

Data Type

Description

Example

String

Represents text enclosed in quotes.

let name = "john";

Number

Represents numeric values (integer or float).

let age = 25;

BigInt

Represents large integers beyond Number.MAX_SAFE_INTEGER.

let bigNum = 12345678901234567890n;

Boolean

Represents true or false values.

let isActive = true;

Undefined

A variable declared but not assigned a value.

let x;

Null

Represents an intentional absence of value.

let data = null;

Symbol

Represents unique and immutable identifiers.

let id = Symbol('id');

Example of Primitive Data Types:
let name = "Alice"; // String
let age = 30; // Number
let bigInt = 9007199254740991n; // BigInt
let isAvailable = true; // Boolean
let notAssigned; // Undefined
let emptyValue = null; // Null
let sym = Symbol('key'); // Symbol

Non-Primitive (Reference) Data Types:

Non-primitive types are mutable and stored as references in memory.

Data Type

Description

Example

Object

A collection of key-value pairs.

let obj = {name: "John"};

Array

A list-like structure for storing multiple values.

let arr = [1, 2, 3];

Function

A block of reusable code.

function greet() { console.log('Hello'); }

Date

Represents dates and times.

let date = new Date();

Example of Non-Primitive Data Types
let person = { name: "Bob", age: 25 }; // Object
let colors = ["red", "blue", "green"]; // Array
function greet() { console.log("Hello!"); } // Function
let currentDate = new Date(); // Date

Dynamic Typing:

JavaScript is dynamically typed, meaning variables can hold values of any type.

Example
let value = 42; // Number
value = "Hello"; // String
value = true; // Boolean

Type Checking:

To check a variable's data type, use the typeof operator:

Example
console.log(typeof "Hello"); // String
console.log(typeof 42); // Number
console.log(typeof true); // Boolean
console.log(typeof undefined); // Undefined
console.log(typeof null); // Object (known JavaScript bug)
console.log(typeof Symbol()); // Symbol
console.log(typeof {}); // Object
console.log(typeof []); // Object (Array is a subtype of Object)
console.log(typeof function(){}); // Function

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.