1

When I run from the browser

typeof undefined

I got "undefined" string. That is why? Most of the time I check my variable something like

if (a == 'undefined') {
  do something
}

is I am checking my variable a is equal to undefined? I don't understand about that. Can someone explain me? Another way to check undefined in javascript?

Set Kyar Wa Lar
  • 3,715
  • 3
  • 27
  • 51

4 Answers4

3

Because that is how typeof works:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

if (a == undefined) { // instead its a type comparison 
Luke Garrigan
  • 2,141
  • 10
  • 20
alessandro
  • 9,507
  • 5
  • 35
  • 46
1

typeof get's you the type as a string.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

Harazi
  • 122
  • 10
0

undefined just happens to be a global name that holds the undefined value.

Quoting the linked page,

undefined is a property of the global object, i.e. it is a variable in global scope.
The initial value of undefined is the primitive value undefined.

typeof always returns a string describing the value of the name passed to it.

AKX
  • 93,995
  • 11
  • 81
  • 98
0

The typeof operator returns a string indicating the type of the unevaluated operand.

It changes like this

Undefined   :"undefined"
Null    :"object"
Boolean :"boolean"
Number  "number"
String  :"string"
Host object (provided by the JS environment)    :Implementation-dependent
Function object (implements [[Call]] in ECMA-262 terms):    "function"
Any other object    :"object"

DOC

chandu
  • 2,198
  • 3
  • 18
  • 35