
x = undefined // Throws a ReferenceError typeof x = 'undefined' // true


Or not, you should use typeof x = 'undefined'. Strictly equal to undefined regardless of whether is has been declared When using x = undefined, JavaScript checks if x is a declared variable Theīiggest difference between these two approaches is that, if x has not been declared, x = undefined throws X = undefined & typeof x = 'undefined' // trueĪnother alternative is checking if typeof x = 'undefined'.

Make sure you use strict equality = to check if a value is In JavaScript, checking if a variable is undefined can be a bit tricky sinceĪ null variable can pass a check for undefined if not written properly.Īs a result, this allows for undefined values to slip through and vice versa.
