What exactly is the function of return in the if statement within the JavaScript function?

Learning the Art of DOM programming

function test(){
//if(!document.getElementById("xx")) return false;
if(variable == 1) return true;
}
console.log(test());

two if statements are in different functions. I understand the first sentence of
in order to make it easier for me to write them together. If the browser does not have this method (true inside the parentheses), exit the entire function.
see the second if today, why do you exit this function when you have true in parentheses? (page 176)
at the same time, if you print test () in the console, you get undefine.... Where on earth did this true go by return?
if there is no if, return directly in the function, you can get the value of return.
I am the younger brother, I am the younger brother, and please do not hesitate to comment.

Jun.08,2022
The

function must have a return value , either explicitly or implicitly.
write return directly, and explicitly return, as long as you run to this statement, you must exit the function.
does not write return , but returns after running to the last sentence of the function body, which is generally equivalent to adding a sentence return; at the end of the function body. (of course, this statement is not absolute, because some higher order functions can omit the explicit return when there is only one statement or expression in the function body and return the function body result directly. In addition, promise returns promise)
js is a weakly typed language, and functions are not typed. does not specify that its function is empty type function, Boolean type function, or pointer function, etc., so the result obtained after function operation is either the return of return, or a special type return, or undefined


return semantics is to end the current function and return a value.
to put it simply, the code below this function will not run again.
in the performance optimization section of the Red Treasure Book, it is proposed that the optimization design of the function is returned as soon as possible.
when the result of the operation is known, or the state of the object is determined, the meaningless running behind is junk time for the program.
at the same time, reasonable early return can improve readability.
of course, you can also return to the same place at the end, which is also highly readable. Don't run meaningless code. As the name implies, the fastest time complexity is 0.


my understanding is that the code is executed sequentially, and when it is executed to return, it will directly return the result after return, and the subsequent code will not continue to execute.

Menu