This question already has answers here:
How does this JavaScript/jQuery syntax work: (function( window, undefined ) { })(window)?
(5 answers)
Next parameter is 'undefined' in jQuery, why? [duplicate]
(3 answers)
Closed 9 years ago.
In John Resig's slideshow on how he was building jQuery 1.4, he mentioned a point where he added an undefined variable to the jQuery closure because "we can re-use (the variable)".
undefined is not an ordinary variable:
> var undefined = 4
undefined
> undefined
undefined
Therefore, we know that undefined is not a variable. So why would an undefined be re-undefined in the jQuery source?
Because in some JavaScript engines it's possible to set undefined to a value. This is to make sure undefined is really undefined.
Additionally to +Rocket Hazmat's answer, you can reduce the file size after compression a bit, when your code uses undefined frequently. That's because a local variable undefined may have its name mangled by the compressor, while the global undefined may not:
foo === undefined;
// ^----- don't touch this, put "undefined" in the compressed result
(function (undefined) {
foo === undefined;
})();
// may however be mangled to
(function(u){foo===u})();
Related
This question already has answers here:
Detecting an undefined object property
(50 answers)
Closed 3 years ago.
I had been checking for undefined values myself like:
if(variable !== 'undefined')
then I came across the following code:
if(typeof variable1 !== 'undefined' || typeof variable2 !== 'undefined')
I followed the same convention knowing it's safer when variable goes undeclared. While in my code reviews, it was pointed I could simple write:
if(variable1 || variable2)
Which of these is the most standard way to check undefined?
There are various use case as following
if (variable) is standard way to check truthiness of any variable in javascript. You can examples of what values will be truthy on Truthy | MDN Web Docs. Also, Falsy | MDN Docs
The cases where you explicitly would check for undefined is when a variable has been declared but not assigned value or explicitly assigned undefined.
In that case use if (variable !== undefined).
If you are receiving response from an API which might consist of stringified value of undefined, which you are sure of, then only do the check if (variable !== 'undefined')
This question already has answers here:
What's the simplest approach to check existence of deeply-nested object property in JavaScript? [duplicate]
(7 answers)
Closed 4 years ago.
This is my code snippet
pastActions = res['data']['list']['names']['blk'].cnt
But I get to see below error
its throws error as Cannot read property 'cnt' of undefined
This is because the parent of cnt is undefined, it might even fail when any of these properties data or list or names or blk is undefined.
I have replaced it as below
res['data'] && res['data']['list] && res['data']['list']['names'] && res['data']['list']['names']['blk'] && res['data']['list']['names']['blk'].cnt ? res['data']['list']['names']['blk'].cnt : '';
and it works, but is there any optimized way to check the same?
you can use lodash _.get function. You have an option to return a default value when you have any of the property in the object as undefined.
check here: lodash.com/docs/4.17.10#get
This question already has answers here:
How can I check for "undefined" in JavaScript? [duplicate]
(16 answers)
Closed 4 years ago.
My Asp masterpage has this code:
<script>
if (theForm !== undefined) { // <<== line 746: error
theForm.onsubmit = ...bla bla... ;
}
</script>
The Chrome console reports the error:
Uncaught ReferenceError: theForm is not defined bla.aspx:746
My question is: is this a wrong way of detecting if a name is undefined? Or is this handled differently by Chrome?
(NB: Firefox console does not report an error, but still stops processing following JS code of this script block)
The message Uncaught ReferenceError: theForm is not defined
should be interpreted as
theForm is not declared
Why?
Basically, a variable can be undefined (but declared) and can be not declared at all.
Example
Declared and undefined
var foo; // if a value would be assigned (i.e.: var foo = 3), then it wouldn't be undefined
console.log(foo);
Not declared (it will throw an error)
console.log(foo); // <-- foo was never declared
How to fix it?
Use typeof like this:
console.log('Is undefined:', typeof foo === 'undefined');
There is a distinction between a variable being declared but having a value of undefined and a variable that was never even declared. I'm guessing your scenario is the latter. Use the typeof operator for this.
if(typeof(theForm) !== 'undefined'){ //typeof returns a string
This question already has answers here:
What is the purpose of passing-in undefined?
(3 answers)
Closed 5 years ago.
I have just come across a function which accepts a parameter actually called 'undefined'. It is something like this:
(function($,undefined) {
// Do something
})(jQuery);
Either I'm going crazy, or there is no logical reason for this to be here as, well, undefined is undefined is undefined. Please can someone confirm either way? Thanks.
That is a classic trick to have an undefined variable to check against, typically:
if (someVar === undefined) {}
// instead of:
if (typeof someVar === 'undefined') {}
Notice that the wrapper IIFE does not pass any second argument, making the undefined parameter effectively undefined.
This question already has answers here:
What does "options = options || {}" mean in Javascript? [duplicate]
(5 answers)
Closed 9 years ago.
So I am looking over a project which includes the following line of javascript:
window.negotiationApp = window.negotiationApp || {};
Can anyone explain might be going on with this line of code?
Update
So now that I understand what this line of code is doing, my question maybe unique in asking the following:
There is no negotiationApp object in the javascript code. window.negotiationApp will always be set to an empty object, it seems the developer is really just using this as a namespace or container for other objects. Is this a common javascript practice?
it makes sure that window.negotiationApp is set. If window does not have this property then it initializes it to be {} (an empty object), otherwise, it does nothing.
It's an idiom that basically means, if window.negotiationApp doesn't exist, set it to {}. You might do that so future info doesn't return undefined or something.
Ensures window.negotiationApp object is not undefined.
window.negotiationApp = window.negotiationApp || {};
Means if window.negotiationApp is defined then use it or assign window.negotiationApp an empty object.
if(window.negotiationApp) {
window.negotiationApp = window.negotiationApp;
}
else {
window.negotiationApp = {};
}
since this variable is set on the global scope, it makes sure not to override an existing one if there is any.
so it basically says, if there is already a negotiationApp variable defined - use it, if not create a new one.