Content deleted Content added
Tags: Reverted possible vandalism |
Tag: Reverted |
||
Line 209:
console.log(undefined === null); // enforce type during check, displays false
</syntaxhighlight>
Try out this example code on the [https://jsonlinecompiler.com/javascript-online-editor/ JavaScript Online Compiler & Editor].
Note: There is no built-in language literal for undefined. Thus {{code|lang=javascript|code=(x === undefined)}} is not a foolproof way to check whether a variable is undefined, because in versions before ECMAScript 5, it is legal for someone to write {{code|lang=javascript|code=var undefined = "I'm defined now";}}. A more robust approach is to compare using {{code|lang=javascript|code=(typeof x === 'undefined')}}.
Line 219 ⟶ 220:
function isUndefined(x) { return (typeof x) === "undefined"; } // ... or that third one
</syntaxhighlight>
Try out this example code on the [https://jsonlinecompiler.com/javascript-online-editor/ JavaScript Online Compiler & Editor].
Here, calling <code>isUndefined(my_var)</code> raises a {{mono|ReferenceError}} if {{mono|my_var}} is an unknown identifier, whereas {{code|lang=javascript|code=typeof my_var === 'undefined'}} doesn't.
|