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() {}