JavaScript Datatypes

By Published 2 min read

In JavaScript, data types define the kind of values that variables can hold and how those values behave. Understanding data types is fundamental to working with JavaScript effectively.

1. Number:

This data type is used for numeric values, which can be integers or floating-point numbers (decimals).

Example :

let age = 25;
let price = 9.99;
Code language: JavaScript (javascript)

2. String:

Strings are used to represent text. You can enclose text in single (‘ ‘), double (” “), or backticks (`) quotes.

Example :

let greeting = "Hello, world!";
let name = 'Alice';
Code language: JavaScript (javascript)

3. Boolean:

Boolean data types have only two possible values: true or false. They are used for logical operations and comparisons.

Example :

let isStudent = true;
let isLoggedIn = false;Code language: JavaScript (javascript)

4. Array:

Arrays are used to store collections of data, such as a list of numbers or strings. Elements in an array are accessed by their position (index) in the array.

Example :

let fruits = ["apple", "banana", "orange"];Code language: JavaScript (javascript)

5. Object:

Objects are used to store key-value pairs, allowing you to create more complex data structures.

Example :

let person = {
    name: "John",
    age: 30,
    isStudent: false
};Code language: JavaScript (javascript)

6. Null:

The null data type represents the intentional absence of any object value. It’s often used to initialize variables that will hold an object later.

Example :

let emptyValue = null;
Code language: JavaScript (javascript)

7. Undefined:

When a variable is declared but hasn’t been assigned a value, it has the undefined data type. It’s different from null in that it indicates a variable’s uninitialized state.

Example :

let jobTitle;
console.log(jobTitle); // Outputs "undefined"
Code language: JavaScript (javascript)

8. Function:

Functions are a special data type in JavaScript, representing executable code blocks. You can declare functions and call them to perform specific tasks.

Exmaple :

function add(x, y) {
    return x + y;
}
Code language: JavaScript (javascript)

9. Symbol:

Symbols are unique and immutable values often used as object property keys to avoid naming conflicts.

Example :

const uniqueKey = Symbol('unique');
let user = {
    name: "Alice",
    [uniqueKey]: 12345
};
Code language: JavaScript (javascript)

10. BigInt:

The BigInt data type is used for large integers that cannot be represented accurately with the standard Number data type. It’s denoted by appending n to a numeric literal like 10n.

Example :

const bigNumber = 1234567890123456789012345678901234567890n;Code language: JavaScript (javascript)

Understanding these data types helps us work with different kinds of information in our JavaScript programs. It’s essential to choose the appropriate data type for each variable or value to ensure our code behaves as intended. JavaScript’s dynamic typing allows variables to change data types during runtime, so it’s crucial to be aware of how data types behave in various contexts.

Khan Waseem is a tech lead and full-stack engineer in Bengaluru, building hospitality and SaaS software with Node.js, React and React Native. See the resume