简体   繁体   English

console.log和Java函数中的return之间有什么区别?

[英]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". 我正在学习,所以我正在解决一个练习,该练习需要脚本来确定输入数字是否为质数,我最初将console.log放入循环中,并在测试数字6时打印出6而不是质数两次,然后打印另一个字符串“ 6是质数”。

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. 但是,我尝试修改该功能,并用return替换console.log,该功能运行良好。

I know return breaks out of the function, but does it also break out of the loop? 我知道return会脱离函数,但它也会脱离循环吗?

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. console.log只会向控制台输出一条消息。 That's it. 而已。

return will exit the currently executing function. return将退出当前执行的函数。

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. return还用于从函数返回值。 You can then use that value in various places such as console.log . 然后,您可以在各个位置(例如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. 以我的理解,return将停止该函数并返回最终结果,而console.log会将其简单地记录到控制台,而不会停止该函数。 So, when your function used return it stopped your function from running the loop completely. 因此,当您使用函数return时,它使函数无法完全运行循环。

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 . 你知道,N作为快是不是素数为n % i === 0true的任何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). 因此,当找到in % i === 0true ,您可以立即返回“ N不是一个质数”,从而退出循环(和函数)。
  • After the for -loop is finished, you return "N is a prime number". for循环完成后,您将返回“ N是质数”。 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. for -loop完成时,您还没有return任何内容,这意味着内部的if -statement从未执行。 This means that n % i === 0 was never true , and therefore N is a prime number. 这意味着n % i === 0永远不会为true ,因此N是质数。
  • 0 and 1 are special cases, which is what the first if -statement is for. 0和1是特殊情况,这是第一个if语句适用的情况。 If a positive number is smaller than 2, you know it can not be prime. 如果正数小于2,则知道它不能为素数。
  • 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 . n % i === 0永远不会对n / 2 + 1n之间的任何itrue ,这意味着您可以将循环的迭代次数停在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. a,现在等于字符串词。

Also, it stops execution of the current function so the console.trace command will never be called. 另外,它会停止执行当前函数,因此将永远不会调用console.trace命令。


"console.log()" on the other hand, writes whatever is inside it's parenthesis to the console (included in the dev tools of most browsers). 另一方面,“ console.log()”将括号内的内容写入控制台(包含在大多数浏览器的开发工具中)。

So, console.log("words phrases nouns") will spit "words phrases and nouns" into the console. 因此, console.log("words phrases nouns")会将“单词短语和名词”吐到控制台中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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