0

Today i caught myself writing the following two lines of code:

if (typeof a === 'undefined') { ... }

if (a === undefined) { ... }

and i was asking myself what exactly the difference is.

Is there any actual difference between these two approaches on checking if a variable is undefined?

Leonard Schütz
  • 439
  • 1
  • 9
  • 14
  • `===` compares the values along with types of a variable, there is no difference – Vishal Sharma Dec 24 '15 at 01:32
  • 1
    [Duplicate](http://stackoverflow.com/questions/4725603/variable-undefined-vs-typeof-variable-undefined) – Stefano Saitta Dec 24 '15 at 01:34
  • 1
    The typeof operator returns a string which will define the type of the value you are evaluating. In this scenario if the var you are evaluating is '31' you can say: if(typeof var === 'number'){ returns true} The == operator will compare for equality. The === operator will not do the conversion, if two values are not the same type === will simply return false – Franco Dec 24 '15 at 01:40

0 Answers0