Course

Web Development
  • 1.1 Introduction to the Web
  • 2.1 HTML Basics
  • 2.2 Text & Content
  • 2.3 Layout & Grouping
  • 2.4 Lists & Tables
  • 2.5 Media & Embedding
  • 2.6 Forms
  • 2.7 Semantic HTML
  • 2.8 Accessibility & SEO
  • 3.1 CSS Introduction
  • 3.2 CSS Syntax
  • 3.3 CSS Selectors
  • 3.4 Typography
  • 3.5 Colors & Backgrounds
  • 3.6 Text Styling
  • 3.7 Box Model
  • 3.8 Units
  • 3.9 Display & Positioning
  • 3.10 Layout Systems
  • 3.11 Responsive Design
  • 3.12 Animations
  • 3.13 Modern CSS
  • 3.14 Performance & Accessibility
  • 3.15 Tailwind CSS Basics
  • 4.1 Introduction to JavaScript
  • 4.2 Variables & Scope
  • 4.3 Data Types
  • 4.4 Type Conversion
  • 4.5 Operators
  • 4.6 Control Flow
  • 4.7 Loops
  • 4.8 Functions
  • 4.9 Advanced Functions
  • 4.10 Objects & Arrays
  • 4.11 The this Keyword

4.3 Data Types

Updated Jul 20, 2026

4.3 Data Types

Updated Jul 20, 2026

A data type defines the kind of value a variable can store.

Examples:

let name = "Samir";

let age = 21;

let isStudent = true;
"Samir" → String
21       → Number
true     → Boolean

What are Data Types?

Data types tell JavaScript:

  • What value is stored
  • How it can be used
  • What operations are allowed

Example:

let age = 21;

JavaScript knows that 21 is a Number, so mathematical operations can be performed on it.


JavaScript Data Types

JavaScript has two categories of data types.

Primitive Types

  • String
  • Number
  • Boolean
  • Undefined
  • Null
  • BigInt
  • Symbol

Non-Primitive Type

  • Object

Data Types Overview

Data Type Example
String "Hello"
Number 100
Boolean true
Undefined undefined
Null null
BigInt 123n
Symbol Symbol()
Object {}

typeof Operator

The typeof operator returns the type of a value.

Syntax:

typeof value

Example:

console.log(typeof "Hello");

Output:

string

String

Strings represent textual data.

Strings can be written using:

Double quotes:

let name = "Samir";

Single quotes:

let city = 'Kathmandu';

Template literals:

let country = `Nepal`;

Example:

let message = "Hello World";

console.log(message);

Output:

Hello World

String Concatenation

Strings can be joined together using the + operator.

let firstName = "Samir";

let lastName = "Niroula";

let fullName =
firstName + " " + lastName;

console.log(fullName);

Output:

Samir Niroula

Template Literals

The modern way to combine strings is with template literals.

let name = "Samir";

console.log(`Hello ${name}`);

Output:

Hello Samir

typeof String

typeof "Hello";

Output:

string

Number

Numbers represent both whole numbers and decimal numbers.

Integer:

let age = 21;

Decimal:

let price = 99.99;

Mathematical Operations

let a = 10;

let b = 5;

console.log(a + b);

Output:

15

Other operations:

10 - 5;

10 * 5;

10 / 5;

10 % 3;

Special Numbers

Infinity

console.log(1 / 0);

Output:

Infinity

NaN

console.log("Hello" * 5);

Output:

NaN

NaN stands for Not a Number.


typeof Number

typeof 100;

Output:

number

Boolean

A Boolean represents logical values.

A Boolean has only two possible values:

  • true
  • false

Example:

let isLoggedIn = true;

let isAdmin = false;

Example:

let isLoggedIn = true;

if (isLoggedIn) {

    console.log("Welcome");

}

Output:

Welcome

typeof Boolean

typeof true;

Output:

boolean

Undefined

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

Example:

let name;

console.log(name);

Output:

undefined

Visual:

Variable Exists

↓

Value Missing

typeof Undefined

typeof undefined;

Output:

undefined

Null

null represents an intentional absence of a value.

Example:

let user = null;

Meaning:

No User Exists Yet

Another example:

let selectedProduct = null;

Null vs Undefined

Undefined:

let name;

Meaning:

Value Not Assigned

Null:

let name = null;

Meaning:

Value Intentionally Empty

typeof Null

typeof null;

Output:

object

This is a historical bug in JavaScript that has been kept for compatibility.


BigInt

BigInt is used to store extremely large integers.

Example:

const big =
123456789012345678901234567890n;

Notice the n at the end.


Why BigInt?

Regular numbers have a maximum safe value.

Number.MAX_SAFE_INTEGER

Output:

9007199254740991

Values larger than this may lose precision.

Use BigInt instead.

Example:

const big =
999999999999999999999999999n;

console.log(big);

typeof BigInt

typeof 100n;

Output:

bigint

Symbol

A Symbol creates a unique value.

Every Symbol is unique.

Example:

const id1 =
Symbol();

const id2 =
Symbol();

console.log(id1 === id2);

Output:

false

Even though both Symbols are empty.


Symbol with Description

const userId =
Symbol("id");

Common uses:

  • Unique IDs
  • Object keys
  • Library development

typeof Symbol

typeof Symbol();

Output:

symbol

Object

Objects store collections of related data.

Example:

const user = {

    name: "Samir",

    age: 21,

    isStudent: true

};

Visual:

user

├── name

├── age

└── isStudent

Accessing Properties

console.log(user.name);

Output:

Samir

Another example:

const car = {

    brand: "Toyota",

    model: "Corolla"

};

console.log(car.brand);

Output:

Toyota

Arrays are Objects

const fruits = [

    "Apple",

    "Mango"

];

typeof fruits;

Output:

object

Functions are Objects

function greet() {

}

typeof greet;

Output:

function

Functions are a special type of object.


Primitive vs Object

Primitive values are stored directly.

let name = "Samir";

Objects are stored by reference.

const user = {

    name: "Samir"

};

Complete Example

let name = "Samir";

let age = 21;

let isStudent = true;

let score;

let product = null;

let big =
999999999999999n;

let id = Symbol();

const user = {

    name: "Samir",

    age: 21

};

typeof Examples

typeof "Hello";

Output:

string
typeof 100;

Output:

number
typeof true;

Output:

boolean
typeof undefined;

Output:

undefined
typeof null;

Output:

object
typeof 100n;

Output:

bigint
typeof Symbol();

Output:

symbol
typeof {};

Output:

object

Data Types Summary

Data Type Example typeof Result
String "Hello" "string"
Number 100 "number"
Boolean true "boolean"
Undefined undefined "undefined"
Null null "object"
BigInt 100n "bigint"
Symbol Symbol() "symbol"
Object {} "object"

Memory Trick

Primitive Types:

String

Number

Boolean

Undefined

Null

BigInt

Symbol

↓

SNBUNBS

Everything else is an Object.


Key Takeaway

JavaScript has 7 primitive data types and 1 major non-primitive data type.

Primitive Types:

  • String
  • Number
  • Boolean
  • Undefined
  • Null
  • BigInt
  • Symbol

Non-Primitive Type:

  • Object

Understanding data types is important because every variable in JavaScript stores one of these types, and the available operations depend on the type of value being stored.

4.2 Variables & Scope4.4 Type Conversion