简体   繁体   中英

Why "console.log" doesn't work after a "return" command?

I was wondering why a console.log command, right after a return command, doesn't work. Here's an example. Shouldn't the console.log(square(50)) execute all the commands in the function body?

With this code, the console.log commands only displays the number 2500, but it doesn't display the phrase "This is the square of it". Why?

 function square(x) { return x * x; console.log(`This is the square of it`); } console.log(square(50));

Because return is a statement and any code written after return doesn't execute that's why console.log written after return statement doesn't execute. The output of above only will be 2500

console.log ( This is the square of it ); this line will be ignored

Because return make the function return immediately, any code written after is totally ignored.

console.log cannot bypass this behaviour.

This is basic programming knowledge, the explanation here will be kept "simple" for the target audience.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

The return statement ends function execution and specifies a value to be returned to the function caller.

The purpose is the return is actually allowing the function to provide some form of information. When the return is reached, the code execution will typically go back to where the function was called from and evaluate to the return value, then continue execution from there:

function sum(a, b) {
    return a + b;
}

const result = sub(3, 2); // Returns 5
console.log(result); // Outputs 5.
console.log(sum(2, 2)); // Outputs 4

Additionally:

function calculate(a, b, operation) {
   const addOperations = ["add", "sum", "plus"];
   const subtractOperations = ["take", "subtract", "minus"];
   const multiplyOperations = ["times", "multiply"];
   const divideOperations = ["divide", "divise"];
   if (addOperations.includes(operation.toLowerCase())) return a + b;
   if (subtractOperations.includes(operation.toLowerCase())) return a - b;
   if (multiplyOperations.includes(operation.toLowerCase())) return a * b;
   if (divideOperations.includes(operation.toLowerCase())) return a / b; 
}

console.log(calculate(3, 3, "multiply")); // Outputs 9
console.log(calculate(4, 4, "divide")); // Outputs 1
console.log(1, 1, ""); // Outputs undefined

If there is no return, it evaluates to undefined.

Because return ends the function , and the console log is ignored.

Instead of use 2 console.log you can use just 1 and call the function after , like this :

function square(x) {
let sq = x*x;
console.log(`This is the square of it: ${sq}`);
}
square(50);

The return statement ends the execution of function at that statement and no further lines will be executed as in your code.

So instead try this:

 function square(x) { return x * x; } console.log('This is the square of it: ', square(50));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM