JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

JavaScript Data Types

5 April 2025 | Category:

In JavaScript, data types represent the kinds of values we can work with and store in variables. Knowing them is essential to write bug-free code.

JavaScript has two main categories of data types:

đź“‚ 1. Primitive Data Types

These are basic, immutable (unchangeable) data types.

Data TypeDescription
StringText data
NumberAny numeric value
BooleanTrue or False
UndefinedVariable declared but not assigned a value
NullIntentionally empty value
Symbol (ES6)Unique and immutable value
BigInt (ES11)Large integers beyond Number limit

🧪 1. String – Text values

let name = "John";
let message = 'Hello';
let sentence = `Welcome, ${name}`;
  • Strings can be in “double quotes”, ‘single quotes’, or template literals (backticks).
  • Template literals support string interpolation using ${}.

🧮 2. Number – Numeric values

let age = 25;
let price = 99.99;
let negative = -10;
  • Includes both integers and floating-point numbers.
  • JavaScript uses only one type for numbers: number.

✅ 3. Boolean – Logical values

let isActive = true;
let isLoggedIn = false;
  • Only two possible values: true or false.
  • Common in conditions (if, while, etc.).

❓ 4. Undefined – Uninitialized variable

let x;
console.log(x); // Output: undefined
  • A variable declared but not assigned gets undefined.

🚫 5. Null – Empty value

let data = null;
  • Represents “no value” or “empty” on purpose.
  • Useful when you want to clear a variable manually.

🧿 6. Symbol – Unique identifiers (ES6)

let id = Symbol("userID");
  • Used to create unique keys in objects, even if values are the same.
  • Mostly used in advanced object handling and libraries.

💯 7. BigInt – Very large integers (ES11)

let bigNumber = 1234567890123456789012345678901234567890n;
  • Can store numbers larger than 2^53 - 1.
  • Add n at the end to declare a BigInt.

đź§± 2. Non-Primitive (Reference) Data Types

These are more complex and store references (not actual values).

TypeDescription
ObjectCollection of key-value pairs
ArrayOrdered list of values
FunctionReusable block of code

🧰 1. Object – Group of key-value pairs

let person = {
  name: "Alice",
  age: 30,
  isStudent: false
};
  • Values are accessed using object.key or object["key"].

📋 2. Array – List of values

let colors = ["red", "green", "blue"];
console.log(colors[0]); // Output: red
  • Arrays use indexes starting from 0.
  • Can store mixed data types too: let mixed = [1, "text", true];

⚙️ 3. Function – Block of reusable code

function greet(name) {
  return `Hello, ${name}`;
}
  • Functions are also objects in JavaScript and can be passed as arguments.

🔍 Checking Data Types

Use the typeof operator:

console.log(typeof "Hello");    // string
console.log(typeof 42);         // number
console.log(typeof true);       // boolean
console.log(typeof null);       // object (quirk in JavaScript)
console.log(typeof undefined);  // undefined
console.log(typeof []);         // object (Arrays are objects)
console.log(typeof {});         // object
console.log(typeof function(){}); // function

đź§  Quick Summary

Data TypeExample
String"Hello", 'Hi'
Number123, 3.14, -99
Booleantrue, false
Undefinedlet x;
Nulllet x = null;
SymbolSymbol("id")
BigInt1234567890n
Object{ name: "John" }
Array[1, 2, 3]
Functionfunction greet() {}