does chrome handle undefined differently? [duplicate] - javascript

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

Related

Optional chaining, but for a variable? [duplicate]

This question already has answers here:
JavaScript check if variable exists (is defined/initialized)
(32 answers)
Closed 12 months ago.
For something like the following:
"use strict";
let a = 1;
console.log(eval(`
a++;
let b=4;
`));
console.log(`a=${a}, b=${b}`);
// ReferenceError: b is not defined
Is it possible to do something where it prints undefined or something if the variable is not there instead of throwing an error? Something conceptually like:
console.log(`a=${a}, b=$?.{b}`)
The placeholder has to contain an expression and there doesn't seem to bean idiomatic way of using an undefined variable (likely due to parsing error) so I would go with a getter instead of using a raw variable.
For example before accessing b you can write
let getB = function () {
return (typeof b !== "undefined")? b :undefined;
}
Then in the template literal you can write ${getB}.

Why initialization of a variable returns undefined in console? [duplicate]

This question already has answers here:
Why does JavaScript variable declaration at console results in "undefined" being printed?
(4 answers)
Closed 5 years ago.
whenever we define a variable in console with some value for example
var f = 20;
var j = 30;
the above statement returns undefined for one time, can you please help in understanding why it returns undefined even it we have defined both the variables?
Secondly if it is related to hoisting then why undefined is coming only once?
The console reports the result of evaluating the expression, equivalent to typeof basically.
typeof eval("var f = 20;");
returns undefined

Uncaught Type Error: Cannot set property of undefined [duplicate]

This question already has answers here:
Why is "this" in an anonymous function undefined when using strict?
(3 answers)
Closed 8 years ago.
Here's the code, it's pretty simple:
(function() {
"use strict";
// Define our constructor
this.White = function() {
this.version = "1.0.0";
};
}());
// Later
a = new White();
alert(a.version);
In JSBin (and when running JShint), it works like it should. Doesn't work in JSBin after adding "use strict". When running this script in Chrome, however, I get this vague message:
Uncaught TypeError: Cannot set property 'White' of undefined
Why?!
using strict mode will not let you create implicit globals.this.White is hoisted globally and it is implicitly created.So removing it will remove the error Strict Mode

Why does one reuse `undefined`? [duplicate]

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})();

Passing undefined parameter to function - check if variable exists [duplicate]

This question already has an answer here:
Error when passing undefined variable to function?
(1 answer)
Closed 9 years ago.
Consider the following Javascript:
function getType(obj){
return(typeof(obj))
}
alert(typeof(obj)) //alerts "undefined" correctly
alert(getType(obj)) //throws an error: ReferenceError: obj is not defined
Why might this be happening? Is there any workaround? I am trying to write a function which checks if a variable exists.
The problem is nothing to do with typeof. The problem is that you cant pass undefined variables to functions.
function doNothing(obj){
}
doNothing(obj);
This code too results in the error: Uncaught ReferenceError: obj is not defined
So it doesn't matter what code you write inside your function, as it won't be called. The error happens before the function call.
typeof is an operator, not a function.
This is why it does not behave in the same way as functions.
typeof is an operator, not a function, and therefore has powers that a function can't have. There's no way to do what you're trying to do.
Your function fails as soon as you try to pass an undefined object. You can't encapsulate the typeof() function. Well, you can, but it will always throw errors when passed undefined objects.

Categories

Resources