How to check if an input is undefined [duplicate] - javascript

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
}

Related

Recursion function javaScript [duplicate]

This question already has answers here:
Recursive function returns undefined
(3 answers)
Closed 1 year ago.
i tried to write this function for guessing if a given number is even or not using recursion,
This is my code :
function isEven(N){
if(N===0){
return true;
}else if(N===1){
return false;
}else {
isEven(N-2);
}
}
console.log(isEven(1)); // gives false
console.log(isEven(0)); //gives true
console.log(isEven(10)); // gives undefined
I dont know why the function is not working except for 0 and 1.
Because the isEven() funtion enters a loop and cannot send anything to the output. Try to change your function and write it in other way

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
}
}

What is the Javascript equivalent of writing 'If not" [duplicate]

This question already has answers here:
Best way to find if an item is in a JavaScript array? [duplicate]
(8 answers)
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 6 years ago.
Python programmer here.
I don't know how to write this. I tried using 'if !in' and '!if in', but I don't know how. Tried to Google it but got no results.
The correct syntax is
if(!condition){
expression();
}
Note that you need parenthesis around the condition.
#plalx wants a formal definition, and here you go:
IfStatement:
if(Expression) Statement else Statement
if(Expression) Statement
In case of any ambiguity the else would be matched with the nearest if.
If you have some value:
var excludeMe = "something unwanted";
Then you can use the following if statement:
if(myTestCase !== excludeMe) { //do something...}
Keep in mind that != does not check type and !== does check type. So, 1 != "1" is false and 1 !== "1" is true.

Prevent stop of code execution on undefined value [duplicate]

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{
// ...
}

Categories