DEV Community

Lokesh
Lokesh

Posted on

Data Types in JavaScript

What Are Data Types?

Data types define the kind of value we store in a variable.

For example, a name is text, age is a number, and a yes-or-no value can be represented using a Boolean.

JavaScript data types are divided into two main categories:

  1. Primitive Data Types
  2. Non-Primitive Data Types

1. Primitive Data Types

Primitive data types represent single values. JavaScript has seven primitive data types.

String

Used to store text.

let name = "John";
Enter fullscreen mode Exit fullscreen mode

Number

Used to store integers and decimal numbers.

let age = 22;
let price = 99.5;
Enter fullscreen mode Exit fullscreen mode

Boolean

Used to store true or false.

let isStudent = true;
Enter fullscreen mode Exit fullscreen mode

Undefined

A variable that has been declared but has not been assigned a value has the value undefined.

let result;
console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output: undefined

Null

Used to represent the intentional absence of a value.

let selectedItem = null;
Enter fullscreen mode Exit fullscreen mode

BigInt

Used to represent very large integers beyond the safe integer range of the regular Number type.

let number = 12345678901234567890n;
Enter fullscreen mode Exit fullscreen mode

Symbol

Used to create unique values, often as unique object property keys.

let id = Symbol("id");
Enter fullscreen mode Exit fullscreen mode

2. Non-Primitive Data Types

Non-primitive values are used to work with collections of data and more complex structures. In JavaScript, these values are objects.

Object

An object stores related information as key-value pairs.

let student = {
    name: "John",
    age: 22
};
Enter fullscreen mode Exit fullscreen mode

Array

An array stores multiple values in an ordered list. Arrays are a special type of object in JavaScript.

let fruits = ["Apple", "Banana", "Mango"];
Enter fullscreen mode Exit fullscreen mode

Function

A function is a reusable block of code that performs a task. Functions are also objects in JavaScript.

function greet() {
    console.log("Hello!");
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)