In the first case, 2ndcondition will be evaluated if condition evaluates to false.
In the second case, because you nested the second if inside the first if, 2ndcondition will never be evaluated. To understand why, I recommend you read about what return statements do.
However, the first case is practically the same as:
if (condition) {
return "condition is true";
}
if (2ndcondition) {
return "second condition is true";
}
They reason why they are the same also has to do with how return works.
As I was saying, read about return (I’ve already given you the link), and trust me, understanding return isn’t hard but is of paramount importance to your later studying
Please do understand that the reason people sometimes don’t give you an answer straight out is because, apart from helping you learn to code, we also want to help you establish your own methods of learning, and the latter is very important if you want to stay in the programming game for the long term.
But in this case I’ll give the answer because I don’t want you to get a bad impression about this forum or be discouraged from coding because of me:
If you click into the link I gave, you’ll see that the first line reads:
The return statement ends function execution and…
This means that, once a function executes a return statement, it’ll ignore whatever follows, including any further if statements.
So, in your nested if example:
if (condition) {
return "condition is true";
if (2ndcondition) {
return "second condition is true";
}}
If condition evaluates to true, it’ll execute return "condition is true"; and ignore if (2ndcondition) {...}
If condition evaluates to false, because if (2ndcondition) {...} is inside if (condition) {...}, the former will not be executed.
if (1stcondition) {
return "condition is true";
if (2ndcondition) {
return "second condition is true";
}}
Does this code work the way I think it does?
If 1stcondition is true, it evaluates whether 2ndcondition is true? But it does not evaluate the 2nd condition if the 1st condition does not resolve to true. Am I correct or am I wrong?
No. That is not correct. The second condition is never run because the return statement immediately stops the function and prevents the next line from running.
Okay, forget the return statement. It is confusing you unnecessarily. Here is the first case:
if (condition) {
console.log("Hello");
} else if (2ndcondition) {
console.log("Good Bye");
}
If condition is true, just Hello is printed because else part is not executed. If condition is false and 2ndcondition is true, then Good Bye is printed. If both conditions are false, nothing is printed.
Here is the second case:
if (condition) {
console.log("Hello");
if (2ndcondition) {
console.log("Good Bye");
}
}
If the condition is true, Hello is printed. And then, if 2ndcondition is true, Good Bye is also printed. If condition is false, nothing is printed. The 2ndcondition won’t get tested because it is inside the first if statement.
Now, if you have
function test() {
if (condition) {
return "Hello";
if (2ndcondition) {
return "Good Bye";
}
}
if the condition is true, return "Hello" is executed. When a return statement is executed, it jumps out immediately from the function. This means the if (2ndcondition) statement won’t get executed at all.
This would never evaluate the 2ndcondition because if the first is true, it would return the value and ignore the following code. If you want it to check the second condition, you could change the order of the lines. However this still means that both the first AND second condition must be true.
if (1stcondition) {
if (2ndcondition) {
return "first AND second condition is true";
}
return "only first condition is true";
}
You should really hear people recommendations, which point you to important matters in your code, you should understand better. Do not underestimate the return statement and look up the way it behaves.
Even if there was no return, the conditions would still run in different manner.
if (condition) {
console.log ("condition is true");
} else if (2ndcondition) {
console.log("second condition is true");
}
In the above code, the second condition will be estimated only if the first condition returns false.
if (condition) {
console.log("condition is true");
if (2ndcondition) {
console.log("second condition is true");
}}
In this last code snippet, the second condition will run only if the first condition return true.
Im sure by now you could figure out this by other people replies