JavaScript Data Types

Primitive Data Types

JavaScript includes these primitive types:

  • string
  • number
  • boolean
  • null
  • undefined
  • symbol
  • bigint
let name = "Alice";      // string
let age = 30;           // number
let isOnline = true;    // boolean
let score = null;       // null
let value;              // undefined
let id = Symbol("id");  // symbol
let big = 123456789012345678901234567890n; // bigint

Non-Primitive Types

Objects, arrays, and functions are reference types:

let person = { name: "Bob", age: 25 }; // object
let colors = ["red", "green"];         // array
function greet() { return "Hi"; }      // function

typeof Operator

typeof checks the data type:

typeof "hello"     // "string"
typeof 42          // "number"
typeof true        // "boolean"
typeof undefined   // "undefined"
typeof null        // "object" (legacy quirk)
typeof {}          // "object"
typeof []          // "object"
typeof function(){} // "function"

Special Values

null and undefined have specific meanings:

let a = null;         // intentional empty
let b;               // undefined
console.log(b);      // undefined

let result = "abc" * 3;
console.log(result); // NaN (Not a Number)

Type Conversion

JavaScript allows type conversion:

String(123)     // "123"
Number("45")     // 45
Boolean(0)       // false
Boolean("text")  // true

// Implicit:
let x = "5" * 2;  // 10 (string converted to number)
let y = "5" + 2;  // "52" (number converted to string)

Best Practices

  • Use === for strict comparisons
  • Check for null and undefined before using variables
  • Avoid relying on type coercion

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic