简体   繁体   中英

what is the difference between console.log and return in a Javascript function?

I am learning so I was solving an exercise which requires a script to determine whether the input number is a prime number or not, I initially placed a console.log in the loop and when the number 6 is tested, it printed out that 6 is not a prime number twice then printed another string "6 is a prime number".

I do not understand why it would such output be given, and why was false/not a prime number printed out twice, why twice precisely? and then once printed true/prime number.

However, I tried to amend the function and replaced the console.log with a return and the function works perfectly well.

I know return breaks out of the function, but does it also break out of the loop?

Although the function works I was just left wondering why this has happened, I might sound silly but I'm still a beginner so I really would appreciate your help, here is my code. Thanks in advance

function prime(n){
    for (var i = 2; i<n; i++){
        if (n%i==0) {
            return n + " is a not a prime number"
        } else if (n%i !== 0 ){
            return n + " is a prime number"
        }
    }
}

console.log just outputs a message to the console. That's it.

return will exit the currently executing function.

Example:

function printStuff() {
  console.log("I'll print out");
  console.log("So will I");
  return;
  console.log("I won't :(");
}

return is also used to, well, return a value from a function. You can then use that value in various places such as console.log .

Example:

function add(x, y) {
  return x + y;
}
var four = add(2, 2);
console.log(four); // 4
console.log(add(four, 2)); // 6

In my understanding, return will stop the function and return a final result, whereas console.log will simple log it to the console, without stopping the function. So, when your function used return it stopped your function from running the loop completely.

This is what you're going for :

function prime(n) {
    if (n < 2) {
        return n + " is a not a prime number";
    }
    for (var i = 2, max = n / 2 + 1; i < max; i++) {
        if (n % i === 0) {
            return n + " is a not a prime number";
        }
    }
    return n + " is a prime number";
}

Explanation :

  • You know that N is a not a prime number as soon as n % i === 0 is true for any i . So when you find an i for which n % i === 0 is true , you can return "N is a not a prime number" immediately, leaving the loop (and the function).
  • After the for -loop is finished, you return "N is a prime number". The very fact that you haven't return ed anything yet when the for -loop is finished, means that the if -statement inside was never executed. This means that n % i === 0 was never true , and therefore N is a prime number.
  • 0 and 1 are special cases, which is what the first if -statement is for. If a positive number is smaller than 2, you know it can not be prime.
  • n % i === 0 will never be true for any i between n / 2 + 1 and n , which means you can cut down the number of iterations for your loop by about half by stopping it at max = n / 2 + 1 .

See this Fiddle for a demo.

"return" sends a value back to caller of the function. eg

var a = function() { 
    return "word";

    console.trace("hellllooooo");
}()

a, now equals the string word.

Also, it stops execution of the current function so the console.trace command will never be called.


"console.log()" on the other hand, writes whatever is inside it's parenthesis to the console (included in the dev tools of most browsers).

So, console.log("words phrases nouns") will spit "words phrases and nouns" into the console.

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