Prevent stop of code execution on undefined value [duplicate] - javascript

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 8 years ago.
Often, I write something like this:
if(model.properties.foo.bar){
// ...
}
else{
// ...
}
Currently, I want to fetch a document only if it isn't already cached. To do so, I write an if-statement like above, checking for the title of the current document.
However, if there is no current document because the user just entered my application, this check will fail due to an undefined model.properties. An error is thrown and the else block won't execute.
What is the commonly best practive to write such checks?

if(model.properties != null){
if (model.properties.foo.bar) {
// ...
} else {
// ...
}
// ...
}
else{
// ...
}

Related

How to check if an input is undefined [duplicate]

This question already has answers here:
Is there a standard function to check for null, undefined, or blank variables in JavaScript?
(47 answers)
How can I check for "undefined" in JavaScript? [duplicate]
(16 answers)
Closed 11 months ago.
So i have a function, lets say:
function a(string).....
and i want to check if the string is undefind. if it is do something
The code would look something like this:
function a(string){
if(string == null){
//do something
}
else{
//do another thing
}
The problem is it doesn't seem to work, even if the string isn't declared its still going to call the else. Maybe we could try doing
if(string == "undefind"){
//do something
}
else{
//do something else
}
but yet it doesn't seem to change anything. null and undefind seem to work in the same way. Is there a specific function from a library that allows the checking if an input is declared? is there a way you could check it directly from the input with something like
if(string.isDeclared()==true){
//do something
}
else{
//do something else
}

How do I prevent TypeError blank is undefined? [duplicate]

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 3 years ago.
Is there an easier way to check if a variable is defined or not in JavaScript when the target is deep within an object?
For example.
// Lets assume this:
response = {
status: "simple-message"
}
// running this:
if (response.data.variable_to_check !== undefined) console.log('undefined');
// will result in this:
> TypeError: response.data is undefined
In PHP I can run the equivalent check:
if (!($response->data->variable_to_check ?? false)) die("Handled Undefined Error");
// will result in this:
> Handled Undefined Error
I know I can iterate manually by checking each item starting with the root to see if it's defined, but that seems tedious. That and wrapping everything in a try/catch.
Is there a cleaner / faster / smarter way to do this?
Use
try{
if(response.data.variable_to_check!==undefined){
console.log("undefined");
}
}
catch(err){console.log(err.message)}

Why does If statement throw an undefined error when I am checking if its undefined? [duplicate]

This question already has answers here:
Access Javascript nested objects safely
(14 answers)
Closed 4 years ago.
Why does my else if statement cause the parser to throw an undefined error? I know that it is undefined which is why I am checking, I want it to hit the else block in this case, because this function is used in mutiple places the data that is being passed in is not always the same. So I am trying to check which type of data is being passed in.
if(icon){
dv.find(".mobCol>.image").html(generateIcon(icon));
}
else if(location[0].properties !== undefined){
dv.find(".mobCol>.image").html(generateIcon(location[0].properties.image));
data = location[0];
}
else{
dv.find(".mobCol>.image").html(generateIcon(location.features[0].properties.image));
data = location.features[0];
}
Because location[0] is undefined. It's a bit odd, but your check should be:
if( location[0] && location[0].properties){ /* .. */}
I've omitted the ===undefined, thats not needed. It tests if it is something is thruthy or falsey
This is a common way to check variables in JS.

Javascript - how to check if the parent object is not undefined? [duplicate]

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 5 years ago.
I find I'm doing this, whereby I need to check that the preceeding variable is not undefined before checking the next one in the chain:
if( this.props.someVar && this.props.someVar.data ) {
// do something with this.props.someVar.data ...
}
It would be ideal just to do this:
if( this.props.someVar.data ) {
// This throws an error because I haven't checked if `this.props.someVar` exists beforehand.
}
Is there an easier/shorter way to so this, preferably using pure Javascript?
I hate that too. The easiest way is to use try catch like this
try {
// do something with this.props.someVar.data
} catch (e) {
if (e instanceof TypeError) {
// TypeError happens when an object in the chain is undefined
}
}

Know if a DOM object is dead [duplicate]

This question already has answers here:
How can I avoid state of "TypeError: can't access dead object" in my Firefox add-on?
(3 answers)
Closed 7 years ago.
I am working with a variable containing a webpage element such as a button.
However, sometimes I get the error "Can't access to a dead object" because the page containing the element has changed since the moment I saved it.
I would like to know a way to check if an element is dead or not, I tried :
if(element)
alert("Do something");
but it doesn't work as expected.
copied from How to check if element exists in the visible DOM?
var elementInDocument = function(element) {
while (element = element.parentNode) {
if (element == document) {
return true;
}
}
return false;
}
You can use it like:
if(elementInDocument(element))
alert("Do something");
//eval it in your mozilla-browser space
var dc = content.document;
content.document.location.reload();
setTimeout(function(){
try{
dc.parentNode;
}catch(e){
if (e.message.indexOf(' dead ')!=-1){
alert('REALY DEAD!');
}
} }, 1000);
it is a test of dead (in try..catch block) in my moz extension projects.

Categories