Typeof operator in JavaScripts

The typeof operator is used to get the data type (returns a string) of its operand. The operand can be either a literal or a data structure such as a variable, a function, or an object.

Syntax: –
typeof operand
typeof(operand)

Ex: –
typeof “a”;

Undefined

The undefined type is used for variable or object properties that either do not exist or have not been assigned a value. The only value an undefined type can have is undefined.

Null

The null value indicates an empty value; it is essentially a placeholder that represents “nothing”. The null value is defined as an empty object so using typeof operator on a variable holding null shows its type to be an object.

Undefined Vs Null

Undefined means the value hasn‟t been set, whereas null means the value has been set to be empty.

var, let and const

var – The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.

let – let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used.

const – This declaration creates a constant whose scope can be either global or local to the block in which it is declared. Global constants do not become properties of the window object, unlike var variables. An initializer for a constant is required; that is, you must specify its value in the same statement in which it’s declared which can’t be changed later.

Tagged : / / / /