How to avoid exception when checking not defined var? [duplicate] - javascript

This question already has answers here:
JavaScript check if variable exists (is defined/initialized)
(32 answers)
Closed 3 years ago.
Simple question but i'm stuck for a while on it.
I need to check if a var is defined, before execute some taks.
So i try this
if (myVar !== null) alert('myVar is not null');
But when i do this and myVar is not defined, an exception is threw :
Uncaught ReferenceError: myVar is not defined at :1:1
So how to solve this dilema ?

You can use type of
if (typeof variable !== 'undefined') alert('myVar is not null');

Related

How can I check if a variable exists (at all) in JS? [duplicate]

This question already has answers here:
JavaScript check if variable exists (is defined/initialized)
(32 answers)
Closed 3 years ago.
Please read the question before marking duplicate. This isn't about an undefined variable. It's about variables which don't exist in the namespace.
I'm working on a codebase which is run in browser and in nativescript. The globals differ between the two. For instance, I'd like to check if window exists at all, but something like this:
if (!!window) {
}
will throw an error:
JS ERROR ReferenceError: Can't find variable: window
Is there a way to test whether or not a variable exists in js (not just undefined)?
You could use a try/catch statement.
try {
if (!!someVariable) {
console.log('yep');
}
} catch {
console.log('nope');
}
You need to use the typeof operator to see if a variable exist or no
you need to test if the variable you are concerned with is undefined of null like so :
if (typeof variable === 'undefined' || variable === null) {
// variable does not exist
}

value === undefined vs typeof value === 'undefined' in node.js [duplicate]

This question already has answers here:
How can I check for "undefined" in JavaScript? [duplicate]
(16 answers)
variable === undefined vs. typeof variable === "undefined"
(9 answers)
Closed 4 years ago.
I have little confusion in this. I am using Auth0 and writing rules which uses node.js syntax. Now I read somewhere that the right way to check undefined value in node is typeof value === 'undefined' but value === undefined also work in node. So what is the main difference in both of them?

Uncaught ReferenceError: Notification is not defined [duplicate]

This question already has answers here:
JavaScript check if variable exists (is defined/initialized)
(32 answers)
Closed 6 years ago.
I thought my object detection would work:
if (Notification!=undefined) {}
However my JavaScript log is still showing the following error:
Uncaught ReferenceError: Notification is not defined
How do I properly do object detection for the Notification object?
No frameworks.
You can use typeof like so:
if (typeof Notification !== 'undefined') {
}
If you use typeof, it does not try to actually use the variable (which breaks and throws an error)

Check if variable is undefined not working [duplicate]

This question already has answers here:
JavaScript check if variable exists (is defined/initialized)
(32 answers)
Closed 6 years ago.
I want to check if a variable is undefined and quickly found some stackoverflow answers that said the correct way to do it is to test if(variable==null). But in Chrome I am getting an error saying:
Uncaught ReferenceError: xdate is not defined
Huh? The whole reason I am testing is so I don't get errors like this. And I did it just like the approved stackoverflow answers.
Here is my code snippet.
if (xdate == null){
var dateadd = "";
} else {
var dateadd = "&date="+date;
}
use something like this to verify whether variable is undefined or not
if (typeof something === "undefined") {
alert("something is undefined");
}

How do I check if an element is undefined? [duplicate]

This question already has answers here:
Detecting an undefined object property
(50 answers)
How can I determine if a variable is 'undefined' or 'null'?
(34 answers)
Closed 9 years ago.
I would like to check to see if a particular attribute of a DOM element is undefined - how do I do that?
I tried something like this:
if (marcamillion == undefined) {
console.log("Marcamillion is an undefined variable.");
}
ReferenceError: marcamillion is not defined
As you can see, the reference error is telling me that the variable is not defined, but my if check is clearly not working, because it is producing the standard js ReferenceError as opposed to the error message I am looking for in my console.log.
Edit 1
Or better yet, if I am trying to determine if the attribute of an element is undefined like this:
$(this).attr('value')
What would be the best way to determine if that is undefined?
Using typeof:
if (typeof marcamillion == 'undefined') {
console.log("Marcamillion is an undefined variable.");
}
Edit for the second question:
if ($(this).attr('value')) {
// code
}
else {
console.log('nope.')
}
if (typeof marcamillion === 'undefined') {
console.log("Marcamillion is an undefined variable.");
}
Note that using === instead of == is considered better style.

Categories