«Back to Home

Learn JavaScript

Topics

Data Types in JavaScript

Every value in JavaScript belongs to a specific data type.

Understanding data types is very important because they help the programming language understand:

  • What kind of data are you working with

  • How that data should behave

  • What operations can you perform on it

For college students and freshers, learning data types early will help avoid many logical errors in programs.

JavaScript has two main categories of data types:

  1. Primitive Data Types

  2. Non-Primitive (Reference) Data Types

Let’s understand both in detail.

Primitive Data Types

Primitive data types store simple values.
They do not store complex structures, and their values cannot be changed directly (they are immutable).

JavaScript has 7 primitive types:

  1. Number

  2. String

  3. Boolean

  4. Undefined

  5. Null

  6. BigInt

  7. Symbol

We will learn each one with examples.

1. Number

Used to store integers and decimals.

let age = 21;
let price = 199.99;

console.log(age);
console.log(price);

Output:

21
199.99

JavaScript does not have separate types for integer and float—both are Number.

2. String

Used to store text.

Strings are written inside quotes:

  • Double quotes " "

  • Single quotes ' '

  • Backticks `` ` (used for template strings)

let name = "Arjun";
let course = 'JavaScript';
let message = `Welcome ${name}`;

console.log(name);
console.log(course);
console.log(message);

Output:

Arjun
JavaScript
Welcome Arjun

3. Boolean

Stores only two values:

  • true

  • false

Example:

let isStudent = true;
let isPassed = false;

console.log(isStudent);
console.log(isPassed);

Output:

true
false

Useful in conditions and decision-making.

4. Undefined

A variable that is declared but not assigned any value becomes undefined.

let x;
console.log(x);

Output:

undefined

It means “value is not given yet”.

5. Null

Null means “empty value” or “no value”.

let data = null;
console.log(data);

Output:

null

You use null when you want to intentionally show that the value is empty.

6. BigInt

Used for very large numbers that cannot be stored in a normal number type.

let bigNumber = 12345678901234567890n;
console.log(bigNumber);

Output:

12345678901234567890n

Notice the n at the end.

7. Symbol

Used to create unique values, mostly in advanced topics.

let id1 = Symbol("id");
let id2 = Symbol("id");

console.log(id1 === id2);

Output:

false

Even though both symbols look the same, they are always unique.

Non-Primitive (Reference) Data Types

These types store collections or complex structures.
They work differently because they are stored by reference, not by value.

Main non-primitive types:

  1. Object

  2. Array

  3. Function

You will learn all of these in upcoming chapters.

Checking Data Type Using typeof

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

Example:

console.log(typeof 25);
console.log(typeof "Hello");
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof null);

Output:

number
string
boolean
undefined
object

Important Point:
typeof null returns object, which is a known JavaScript mistake but kept for backward compatibility.

Example Program With All Data Types

let studentName = "Riya";       // string
let age = 20;                   // number
let isEnrolled = true;          // boolean
let marks;                      // undefined
let address = null;             // null
let largeValue = 9876543210n;   // BigInt
let uniqueId = Symbol("id");    // symbol

console.log(typeof studentName);
console.log(typeof age);
console.log(typeof isEnrolled);
console.log(typeof marks);
console.log(typeof address);
console.log(typeof largeValue);
console.log(typeof uniqueId);

Output:

string
number
boolean
undefined
object
bigint
symbol

Why Understanding Data Types Matters

Data types help you:

  • Store values correctly

  • Perform accurate calculations

  • Avoid type errors

  • Understand how JavaScript behaves

  • Build strong logic for conditions

As your programs get bigger, data types become more important.

Practice Task (Do It Yourself)

Create variables of all primitive data types:

  • A number

  • A string

  • A boolean

  • An undefined variable

  • A null variable

  • A BigInt

  • A symbol

Print both the value and its type using typeof.

Author
Vijayakumar S
0 3.9k 2m