window.undefined is writable, use void(0)
The variable window.undefined is writable in a number of commonly used browsers. This can create confusion, but there is a simple way to get around it.
void(0) always returns the primitive value of undefined independently of what window.undefined is set to.
Note that this may affect the performance a bit since another function call has to be made. However, in most cases this can be neglected.
Written by Jesper Petersson
Related protips
3 Responses
You can also wrap your code in an IIFE and create a local undefined.
(function(window, undefined){
alert( typeof undefined ); // 'undefined'
}(window)),@kevincennis yeah that's how jQuery does it!
I think is more readable just to compare undefined as string
typeof somevar === "undefined"
Have a fresh tip? Share with Coderwall community!
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside