Chapter 05 of 32

JavaScript Data Types

Whenever we create a variable in JavaScript, we store some kind of value in it. That value could be a number, text, a true/false value, or something more complex.

These different kinds of values are called data types.

What is a Data Type?

A data type defines the kind of value a variable can hold and the operations that can be performed on that value.

For example:

let name = "John";
let age = 20;
let isStudent = true;

Here:

  • "John" is a String

  • 20 is a Number

  • true is a Boolean

JavaScript has several built-in data types.


JavaScript Data Types

JavaScript data types can broadly be divided into two categories:

Category

Data Types

Primitive

String, Number, BigInt, Boolean, Undefined, Null, Symbol

Non-Primitive

Object

Let's understand them one by one.


Primitive Data Types

Primitive values are the basic building blocks of JavaScript.

1. String

A String is a sequence of characters used to represent text.

Strings can be written using single quotes, double quotes, or backticks.

let name = "John";
let city = 'Delhi';
let message = `Hello World`;

All three are strings.

You can also combine strings:

let firstName = "John";
let lastName = "Smith";

console.log(firstName + " " + lastName);

Output:

John Smith

We'll learn more about strings later.


2. Number

The Number type is used for both integers and decimal numbers.

let age = 21;
let price = 99.99;
let temperature = -5;

JavaScript doesn't have separate int and float types like some other languages.

All of these are simply Number values.

let a = 10;
let b = 5.5;

console.log(a + b);

Output:

15.5

3. BigInt

Sometimes numbers can become extremely large—larger than the range safely represented by JavaScript's normal Number type.

For such cases, JavaScript provides BigInt.

let largeNumber = 123456789012345678901234567890n;

Notice the n at the end.

console.log(largeNumber);

BigInt is mainly useful when working with very large integer values, such as certain financial, scientific, or cryptographic calculations.


4. Boolean

A Boolean represents one of two values:

true
false

For example:

let isLoggedIn = true;
let isAdmin = false;

Booleans are commonly used when making decisions.

let age = 20;

console.log(age >= 18);

Output:

true

We'll use Boolean values heavily when learning conditional statements.


5. Undefined

A variable has the value undefined when it has been declared but hasn't been assigned a value.

let name;

console.log(name);

Output:

undefined

In other words, JavaScript is basically saying:

"This variable exists, but no value has been assigned to it yet."


6. Null

null represents the intentional absence of a value.

let selectedUser = null;

Here, we're deliberately saying that selectedUser currently has no value.

This is different from undefined.

A simple way to remember it:

undefined → No value has been assigned
null      → We intentionally set it to no value

7. Symbol

A Symbol is a special primitive data type used to create unique values.

let id = Symbol("id");

Even if two Symbols contain the same description, they are still different:

let a = Symbol("id");
let b = Symbol("id");

console.log(a === b);

Output:

false

Symbols are mostly useful in advanced JavaScript, especially when working with objects and unique property keys. You don't need to worry too much about them as a beginner.


Non-Primitive Data Type

8. Object

An Object is used to store collections of related data and functionality.

For example:

let student = {
    name: "John",
    age: 20,
    city: "Delhi"
};

Here, student contains multiple pieces of information.

We can access them using their properties:

console.log(student.name);
console.log(student.age);

Output:

John
20

Arrays, functions, and many other complex values are also objects in JavaScript.

For example:

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

Arrays are used to store multiple values in an ordered collection.

We'll learn about objects and arrays separately in much more detail.


Checking a Data Type with typeof

JavaScript provides the typeof operator to check the type of a value.

let name = "John";
let age = 20;
let isStudent = true;

console.log(typeof name);
console.log(typeof age);
console.log(typeof isStudent);

Output:

string
number
boolean

Some more examples:

console.log(typeof 100);
console.log(typeof "Hello");
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof 10n);

Output:

number
string
boolean
undefined
bigint

A Small JavaScript Oddity

There's one famous quirk beginners often notice:

console.log(typeof null);

The result is:

object

Even though null is actually a primitive value. This is a long-standing behavior of JavaScript, so just remember that typeof null returns "object".


Primitive vs Non-Primitive

Here's a simple comparison:

Primitive

Non-Primitive

String

Object

Number

Array

BigInt

Function

Boolean

Other object-based values

Undefined

Null

Symbol

Primitive values represent individual basic values, while objects can represent more complex collections of data.


A Practical Example

Let's use different data types together:

const name = "John";
let age = 21;
const isStudent = true;
let address = null;

console.log(name);
console.log(age);
console.log(isStudent);
console.log(address);

Here we have:

Variable

Value

Data Type

name

"John"

String

age

21

Number

isStudent

true

Boolean

address

null

Null

This is how you'll normally work with data types in real JavaScript programs—different variables holding different kinds of information.

Conclusion

Data types tell JavaScript what kind of value we're working with. The most important types to remember initially are:

String
Number
Boolean
Undefined
Null
Object

JavaScript also provides BigInt and Symbol for more specialized situations.

Once you understand data types, the next step is learning how JavaScript handles values, operations, and expressions, which leads naturally to JavaScript Operators.