What Are Data Types In JavaScript? See Examples

JavaScript works with different kinds of values, and each value belongs to a specific data type. Understanding data types helps you write cleaner, bug-free code and manage your program’s logic effectively.

In this guide, we’ll explore all JavaScript data types with easy explanations and examples.


What Are Data Types?

A data type defines the type of value a variable can hold.
JavaScript has two main categories:

Primitive Data Types — simple & immutable

Non-Primitive Data Types — complex & mutable

Let’s look at each in detail.


Primitive Data Types (7 Types)

Primitive values are stored directly and do not change once created.

JavaScript has 7 primitive data types:

  1. String
  2. Number
  3. Boolean
  4. Null
  5. Undefined
  6. Symbol
  7. BigInt

1. String

Used to store text or characters.

Example

let name = "Sagar";
let message = 'Hello World!';
let greeting = `Welcome, ${name}`;

2. Number

Represents integers and floating-point numbers.

Example

let age = 22;
let price = 99.50;

JavaScript doesn’t differentiate between int, float, or double — everything is a number.


3. Boolean

Represents logical values — true or false.

Example

let isOnline = true;
let isAdmin = false;

4. Null

Represents an intentionally empty value.

Example

let data = null; // empty on purpose

5. Undefined

A variable declared but not assigned a value.

Example

let x;
console.log(x); // undefined

6. Symbol (ES6)

Unique and immutable values, commonly used as object keys.

Example

let id = Symbol("userID");

7. BigInt (ES2020)

Used for extremely large numbers beyond the safe integer limit.

Example

let bigNumber = 987654321987654321987654321n;

Non-Primitive Data Types

JavaScript has one non-primitive type: Object.

Objects can store complex data and multiple values.

Non-primitive types include:

  • Objects
  • Arrays
  • Functions
  • Dates
  • RegExps

1. Object

Stores data in key-value pairs.

Example

let user = {
  name: "Sagar",
  age: 22,
  city: "Ludhiana"
};

2. Array

Stores ordered collections of values.

Example

let fruits = ["Apple", "Banana", "Mango"];

3. Function

Functions are also treated as objects.

Example

function greet() {
  return "Hello!";
}

Type Checking Using typeof

You can check the type of a value using JavaScript’s typeof operator.

Example

typeof "Hello"        // "string"
typeof 42             // "number"
typeof true           // "boolean"
typeof undefined      // "undefined"
typeof null           // "object"   // known JS bug
typeof {}             // "object"
typeof []             // "object"
typeof function(){}   // "function"

Conclusion

Data types are the foundation of JavaScript programming.
Understanding primitive and non-primitive types helps you manage variables correctly, prevent bugs, and write better code.


📌 Citations

🔗 View other articles about Javascript:
https://savanka.com/category/learn/js/

🔗 External Javascript Documentation:
https://www.w3schools.com/js/

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *