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

what's the difference between return and console log
console log returns function
what are the implications of the ability of a function to return a value?
console log response
console log separator
console log result
console log vs print
nodejs return

I get different outputs when I use console.log() in my function vs when I use the return statement.

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?

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 evaluates its argument (if supplied) and ends the execution of the containing function immediately.

console.log evaluates its argument and prints it, continuing the execution of the function it's called from.

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.

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');

Differences between console.log and return?, <script type = "text/javascript"> function tryConsole() { console.log("hello world"); } </script>. When using the return statement, the function will  Console.log emits a message to your browsers Console, and is usually used to emit debugging messages (research your browser's developer tools) return is a keyword that terminates a function and possibly returns a value to the caller of that function.


You are correct in stating that it is because the return statement exits the function.

This is also the case in python,java, and many other languages.

Cheers!

Difference between console.log and return in javascript?, in the code above, I use "return" as a mechanism for sending a value back to the calling function. So when I call foo(), the value 5 is returned. But I don't know that,​  So as you can see, the two concepts are completely unrelated. return is a statement that allows a function to output a value back to where it was called. console.log is a function that lets us inspect values for debugging purposes. Note also that return is a standard ECMAScript feature - it is defined by the language specification.


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.logs the FizzBuzz test result each time.

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.

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.

What is the difference between console.log and return in JavaScript , Hi Dmitri. There are couple of differences between console.log() and return. First is that return will not appear in console. Second and more  var nameString = function (name) { return "Hi, I am" + " " + name } console.log(nameString("Susie")) Im confused about the terminology used. What exactly is the difference between using console.log and using return.


Yes, you are thinking on the correct lines.

A return statement in a function will return a value and stop further execution. Where as Console.log() is a side effect producing function that will print the arguments supplied to it in the console.

Having a console.log() inside a function is like calling a function within a function.

Hello, i can't understand what's the difference between console.log , return 0;. 6. } 7. ​. 8. console.log(getRectArea(3, 4));. 9. // expected When a return statement is used in a function body, the execution of the  I see that alert() and console.log() can return different results. I assumed that they were similar functions but just returned in different places. Back story: my boss likes to see alerts() during development but I can't get the object details in the alert (at least not easily).


return, Javascript will add the numbers first, and then execute the return. We'll talk about the difference between return and console.log() in a bit. For now, let's invoke  The console.log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console.log(A); Parameters: It accepts a parameter which can be an array, an object or any message. Return value: It returns the value of the parameter given.


Intro to Coding, function foo() { const secret = Math.trunc(Math.random()*100) return function inner() { console.log('The secret number is ${secret}.') } } const f  Hi, I have no idea what you're talking about for most of this as return and console.log do completely different things. Return is used to leave a function and give the value being returned to the place the function was originally called from. Console.log is used to log a message in the console.


JavaScript basic: Get the difference between a given number , Let's try something a little different: let's rewrite our say() function from above so that now instead of console.log() -ing what it says, it returns it. function say(​greeting  Difference between log() and dir() You might ask yourself what's the difference between console.dir() and console.log(). Another useful difference in Chrome exists when sending DOM elements to the console. Notice: console.log prints the element in an HTML-like tree; console.dir prints the element in a JSON-like tree