JavaScript Data Types

Learn about the different data types in JavaScript.

Introduction to Data Types

Data types define the kind of data a variable can hold in JavaScript. Understanding them is crucial for writing clean and bug-free code.

JavaScript variables can hold different data types, such as numbers, strings, objects, and more.

Primitive Data Types:

JavaScript provides the following primitive types:

  • Number: Represents both integer and floating-point numbers(e.g., 42, 3.14)
  • String: Represents text(e.g., "Hello")
  • Boolean: Represents true or false
  • Undefined: A variable that has not been assigned a value
  • Null: Represents the intentional absence of any object value
  • Symbol: A unique and immutable primitive value(used in advanced patterns)
  • BigInt: Represents integers with arbitrary precision(newer in JS)

Examples:


let name = "Alice";      // String
let age = 25;            // Number
let isStudent = true;    // Boolean
let score = null;        // Null
let city;                // Undefined
            

Reference Data Types:

  • Object: Collections of properties
  • Array: Ordered lists of values
  • Function: Blocks of code designed to perform tasks

let person = { name: "Bob", age: 30 }; // Object
let colors = ["red", "green", "blue"]; // Array
function greet() { alert("Hello!"); }  // Function
            

🔍 typeof Operator

The typeof operator can be used to check the data type of a variable:


console.log(typeof "text");     // string
console.log(typeof 123);        // number
console.log(typeof true);       // boolean
console.log(typeof {});         // object
console.log(typeof []);         // object (arrays are objects)
console.log(typeof null);       // object (quirk in JS)
console.log(typeof undefined);  // undefined
            

Important Notes

  • null is of type object (JavaScript bug)
  • Arrays and functions are technically objects
  • JavaScript is dynamically typed – data types can change at runtime

Practice Task

Declare different data types and print them using typeof:


let language = "JavaScript";
let version = 2025;
let isFun = true;
let unknown;
let empty = null;

console.log(typeof language);
console.log(typeof version);
console.log(typeof isFun);
console.log(typeof unknown);
console.log(typeof empty);