Call typeof operator on undefined

Description

Only one useful operation can be performed on an undeclared variable: you can call typeof on it.

Calling delete on an undeclared variable won't cause an error, but throws an error in strict mode.

typeof returns undefined for an uninitialized variable, but it also returns undefined for an undeclared variable, which can be a bit confusing.

Example

Consider this example:


var message;     //this variable is declared but has a value of undefined
console.log(typeof message);  //"undefined"
console.log(typeof age);      //"undefined"

Note

You should always initialize variables. Therefore when typeof returns undefined, you'll know that a given variable hasn't been declared rather than was not initialized.





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions