The typeof operator produces a string identifying a value’s JavaScript type category. It is useful when checking what a dynamically typed variable currently contains. section 3.3.1 section 7.2
Inspecting a changing variable
let myVar; console.log(`${myVar} = ${typeof myVar}`); myVar = 12; console.log(`${myVar} = ${typeof myVar}`); myVar = "Hello"; console.log(`${myVar} = ${typeof myVar}`); myVar = true; console.log(`${myVar} = ${typeof myVar}`); This produces undefined = undefined, 12 = number, Hello = string, and true = boolean. The reported type changes because the value changes.
Use the result as a runtime string, while keeping these boundaries in mind:
- An unassigned variable produces undefined, which distinguishes it from assigned values. section 7.2
- typeof null produces object, a longstanding compatibility oddity rather than a precise null classification. section 3.3.1
- In TypeScript, typeof can serve as an alternative to a type assertion for JavaScript primitive types. section 7.8


