GGistDev

Values and Types in JavaScript

JavaScript has dynamic types and a small set of primitives.

Primitives

  • number (IEEE 754, includes NaN/Infinity)
  • bigint (arbitrary precision integers)
  • string
  • boolean
  • null
  • undefined
  • symbol (unique identifiers)
const n = 42
const f = 3.14
const bi = 123n
const s = "hello"
const b = true
const v = null
let u // undefined
const sym = Symbol('id')

Objects

Everything else is an object (arrays, functions, dates, etc.). Arrays are objects with special behavior; functions are callable objects.

const obj = { a: 1 }
const arr = [1,2,3]
function fn(x) { return x * 2 }

typeof and quirks

typeof 42          // 'number'
typeof 123n        // 'bigint'
typeof null        // 'object' (historical quirk)
typeof undefined   // 'undefined'
typeof {}          // 'object'
typeof []          // 'object'
typeof fn          // 'function'

Notes:

  • Numbers are double‑precision floats; use Number.isNaN() and Number.isFinite()
  • BigInt cannot be mixed with Number in arithmetic without conversion
  • Symbols are unique even with the same description

Equality

Use strict equality === to avoid coercion surprises; Object.is handles NaN and signed zero correctly.

0 == false   // true (coercion)
0 === false  // false
null == undefined   // true
null === undefined  // false
Object.is(NaN, NaN) // true (unlike ===)
Object.is(0, -0)    // false (=== considers them equal)

Truthiness

Falsy: false, 0, -0, 0n, "", null, undefined, NaN. Everything else is truthy.

if ("0") { /* truthy */ }

Conversions

Number("10")   // 10
String(123)    // '123'
Boolean(0)     // false
parseInt("08", 10) // 8 (always pass radix)

JSON and structured data

const obj2 = JSON.parse('{"a":1}')
const txt = JSON.stringify(obj2)

Summary

  • Prefer ===/!== and explicit conversions
  • Know falsy values; remember typeof null === 'object' quirk; use Object.is for NaN and signed zero