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
returnwill halt the function and return immediately. The remaining body of code in the function will not be executed.In your example,
numis assigned1, so the condition inside your function istrue. This means that your function will return there and then withtrue.You could also rewrite that function so its body is
return (num == 1).