JavaScript syntax: Difference between revisions

Content deleted Content added
Nelsonkam (talk | contribs)
For … in loop: Improved formatting for better rendering on mobile version.
Tags: Mobile edit Mobile web edit
Nelsonkam (talk | contribs)
Undefined: Improved formatting for better rendering on mobile version.
Tags: Mobile edit Mobile web edit
Line 148:
</syntaxhighlight>
 
Note: There is no built-in language literal for undefined. Thus <ttcode>(x == undefined)</ttcode> 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 <ttcode>var undefined = "I'm defined now";</ttcode>. A more robust approach is to compare using <ttcode>(typeof x === 'undefined')</ttcode>.
 
Functions like this won't work as expected:
Line 158:
</syntaxhighlight>
 
Here, calling <ttcode>isUndefined(my_var)</ttcode> raises a <tt>{{mono|ReferenceError</tt>}}, if <tt>{{mono|my_var</tt>}} is an unknown identifier, whereas <ttcode>typeof my_var === 'undefined'</ttcode> doesn't.
 
===Null===