JS Basics

JavaScript Data Types

Explore JavaScript primitive types, objects, and how JavaScript handles type coercion.

JavaScript Data Types

JavaScript has 8 data types:

Primitive types:

  • string — text
  • number — integers and floats (64-bit floating point)
  • bigint — arbitrarily large integers
  • boolean — true or false
  • undefined — uninitialized variable
  • null — intentional absence of value
  • symbol — unique identifier

Reference types:

  • object — collections of key-value pairs (includes arrays and functions)

Type Checking

Use typeof to check the type of a value.

Type Coercion

JavaScript automatically converts types in some situations (implicit coercion). This can be surprising — use === (strict equality) instead of == to avoid unexpected type coercion.

Example

javascript
// Primitive types
const str = "Hello";           // string
const num = 42;                // number
const big = 9007199254740993n; // bigint
const bool = true;             // boolean
let undef;                     // undefined
const empty = null;            // null

// Type checking
console.log(typeof str);   // "string"
console.log(typeof num);   // "number"
console.log(typeof bool);  // "boolean"
console.log(typeof undef); // "undefined"
console.log(typeof null);  // "object" (JS quirk!)
console.log(typeof {});    // "object"
console.log(typeof []);    // "object"
console.log(typeof function(){}); // "function"

// Type coercion examples
console.log("5" + 3);   // "53" (string concat)
console.log("5" - 3);   // 2 (numeric)
console.log(true + 1);  // 2

// Use strict equality
console.log(0 == false);  // true (loose)
console.log(0 === false); // false (strict)
console.log(null == undefined);  // true
console.log(null === undefined); // false
Try it yourself — JAVASCRIPT