简体   繁体   English

return和console.log()有什么区别

[英]What is the difference between return and console.log()

I get different outputs when I use console.log() in my function vs when I use the return statement. 当我在函数中使用console.log()与使用return语句时,得到不同的输出。

When I run the function with the return statement I get a one word output which is one of the following: 'fizz' 'buzz' or 'fizzbuzz', but when I run the function using console.log the output is counting to the limit and saying 'fizz' 'buzz' or 'fizzbuzz' whenever it comes across a multiple of 3, 5 or both/ why is this so? 当我用return语句运行该函数时,我得到一个单词输出,它是以下之一:'fizz''buzz'或'fizzbuzz',但是当我使用console.log运行该函数时,输出计数到极限并在遇到3、5或两者的倍数时说“嘶嘶声”,“嗡嗡声”或“嘶嘶声” /为什么会这样?

input = fizzBuzz(100)
console.log(input)

function fizzBuzz(limit){
    for (let i = 0; i <= limit; ++i)
    if (i % 3 === 0 && i % 5 === 0) 
    console.log('fizzbuzz')
    else if (i % 3 === 0)
    console.log('fizz')
    else if (i % 5 === 0)
    console.log('buzz')
    else console.log(i)
}


input = fizzBuzz(100)
console.log(input)

function fizzBuzz(limit){
    for (let i = 0; i <= limit; ++i) {
        if (i % 3 === 0 && i % 5 === 0)
            return 'fizzbuzz'
        else if (i % 3 === 0)
            return 'fizz'
        else if (i % 5 === 0)
            return 'buzz'
        else return i
    }
}

I think it is because the return statement stops the function from executing anything further but I am not sure, still new and self teaching! 我认为这是因为return语句使函数无法继续执行任何操作,但是我不确定,这仍然是新的并且是自学的!

You are correct in stating that it is because the return statement exits the function. 您正确地说这是因为return语句退出了该函数。

This is also the case in python,java, and many other languages. 在python,java和许多其他语言中也是如此。

Cheers! 干杯!

Here's what each different function does: 这是每个不同功能的作用:

function fizzBuzz(limit) {
    for (let i = 0; i <= limit; ++i)
        if (i % 3 === 0 && i % 5 === 0)
            console.log('fizzbuzz')
        else if (i % 3 === 0)
            console.log('fizz')
        else if (i % 5 === 0)
            console.log('buzz')
        else console.log(i)
}

This iterates through to limit , and console.log s the FizzBuzz test result each time. 这会遍历到limit ,并且console.log每次都会显示FizzBu​​zz测试结果。

function fizzBuzz(limit){
    for (let i = 0; i <= limit; ++i) {
        if (i % 3 === 0 && i % 5 === 0)
            return 'fizzbuzz'
        else if (i % 3 === 0)
            return 'fizz'
        else if (i % 5 === 0)
            return 'buzz'
        else return i
    }
}

This iterates through to limit , and returns a single value from the function - the result of the FizzBuzz test on the first iteration. 这会迭代到limit ,并从函数中返回一个值-第一次迭代时FizzBu​​zz测试的结果。

So essentially, one function logs all the FizzBuzz results to the console, and the other returns one FizzBuzz result, which you are logging to the console manually. 因此,从本质上讲,一个函数将所有FizzBu​​zz结果记录到控制台,而另一个函数则返回一个FizzBu​​zz结果,您要手动将其记录到控制台。

return evaluates its argument (if supplied) and ends the execution of the containing function immediately. return计算其参数(如果提供)并立即结束包含函数的执行。

console.log evaluates its argument and prints it, continuing the execution of the function it's called from. console.log评估其参数并打印它,继续执行从其调用的函数。

So in your example with return , the fact that you're in a for-loop that goes limit times doesn't matter, because you'll only go through it once and return from fizzBuzz . 因此,在带有return的示例中,您处于进入limit时间的for循环中这一事实并不重要,因为您只需经历一次并从fizzBuzz return fizzBuzz

Putting it all together with another example: 将其与另一个示例放在一起:

function print_dog() {
  console.log('dog');
  return;
  console.log('cat');
}

If you then call print_dog() , you'll see the output dog , but you won't see the output cat , because return ends execution of print_dog before it can get to console.log('cat'); 如果您随后调用print_dog() ,则会看到输出dog ,但不会看到输出cat ,因为return在将print_dog转到console.log('cat');之前执行结束console.log('cat');

Yes, you are thinking on the correct lines. 是的,您正在考虑正确的路线。

A return statement in a function will return a value and stop further execution. 函数中的return语句将返回一个值并停止进一步执行。 Where as Console.log() is a side effect producing function that will print the arguments supplied to it in the console. 其中,Console.log()是产生副作用的函数,它将打印在控制台中提供给它的参数。

Having a console.log() inside a function is like calling a function within a function. 在函数内具有console.log()就像在函数内调用函数。

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

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