can i ask you guys a question? Below is my code:
var num = 1;
var isNumberEqualOne = function(){
if(num == 1){
return true;
}
return false;
}();
alert(isNumberEqualOne);
In this code, the statement in the function return true, after return true, the code in the function is still executing right? So at the end, the code meet return false, so why the function still return true.Sorry for my bad english.Thanks
return
will halt the function and return immediately. The remaining body of code in the function will not be executed.
In your example, num
is assigned 1
, so the condition inside your function is true
. This means that your function will return there and then with true
.
You could also rewrite that function so its body is return (num == 1)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With