Home Courses Data Types Explained - Primitive vs Reference

Data Types Explained - Primitive vs Reference

We are learning data types, which are closely related to variables.

A variable works like a container, and the data type tells us what kind of value is stored inside that container.


For example:

  1. A container can hold water, sugar, or gas
  2. Similarly, a variable can store text, numbers, or true/false values


What are Data Types?

Data types define the type of value stored inside a variable.

Examples:

  1. "Anil" → String
  2. 29 → Number
  3. true → Boolean


Types of Data Types

JavaScript has two main categories:

1. Primitive Data Types (Simple Values)

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


2. Reference Data Types (Complex Values)

  1. Object
  2. Array
  3. Function
  4. Date
  5. Map / Set


Primitive Data Types Explained

1. String

Used to store text. Always written inside quotes.

let name = "Anil";

2. Number

Used to store numeric values.

let age = 29;

3. Boolean

Used for true/false values.

let isOnline = true;

4. Undefined

Variable is declared but no value assigned.

let data;

5. Null

Represents intentionally empty value.

let value = null;

6. BigInt

Used for very large numbers.

let bigNumber = 12345678901234567890n;

7. Symbol

Used for unique values (advanced concept).


Reference Data Types

These store multiple or complex values.

Object Example

let data = {
name: "Anil",
age: 29
};


typeof Operator

To check the data type of a variable:

typeof variableName;


Code Example

<html>
<head>
<script>
// let name="Anil sidhu";
let name="true"
// name=true
// let age=29;
// let isOnline=false
// let data={name:'anil',age:29};
//alert(age)
console.log(typeof name)
</script>
</head>
<body>
<h1>Data Type in JavaScript</h1>
</body>
</html>

Explanation of Code

Case 1

let name = "true";
console.log(typeof name);

Output:

string

Reason:

  1. Value is inside quotes
  2. So it is treated as a string

Case 2 (Commented in your code)

// name = true

If used:

Output:

boolean

Reason:

  1. No quotes
  2. So it is a boolean value


Dynamic Typing in JavaScript

JavaScript allows changing the type of a variable:

let name = "Anil"; // string
name = 30; // number
name = true; // boolean


Common Mistakes

1. Using quotes with numbers


let num = "20"; // string, not number


2. Confusing string and boolean

let value = "true"; // string (wrong if boolean needed)
let value = true; // boolean (correct)


3. Not checking type

console.log(typeof value);


Share this lesson: