JavaScript Data Types & typeof Operator

๐Ÿ“˜ What are Data Types?

In JavaScript, data types describe the kind of value a variable can hold. JavaScript is a dynamically typed language, meaning you don't need to declare the type explicitly.

๐Ÿ“Š JavaScript Data Types

  • String โ€“ Text values
  • Number โ€“ Integers and decimals
  • Boolean โ€“ true or false
  • Undefined โ€“ Variable declared but not assigned
  • Null โ€“ Intentional empty value
  • Object โ€“ Collection of key-value pairs
  • Array โ€“ List of values

๐Ÿ” typeof Operator

The typeof operator is used to check the data type of a variable.

typeof variableName;

๐Ÿ’ป Example Code

let name = "Ahmed";
let age = 25;
let isStudent = true;
let city;
let score = null;
let user = { name: "Ahmed", age: 25 };
let colors = ["red", "green", "blue"];

console.log(typeof name);      // string
console.log(typeof age);       // number
console.log(typeof isStudent); // boolean
console.log(typeof city);      // undefined
console.log(typeof score);     // object (JavaScript quirk)
console.log(typeof user);      // object
console.log(typeof colors);    // object
            

๐Ÿงช Try It Yourself

Click the button to see the data types in action: