Why does it print the return value to the console without console.log? And with quotes included?


#1



https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/0/1?curriculum_id=506324b3a7dffd00020bf661


Ok, I have some basic to moderate experience in other languages. My understanding from that experience, AND in everything I read elsewhere about Javascript, is that 'return' should NOT actually print anything to the console(or elsewhere) on its own. Yet that's exactly what's happening with this code.

Also, why is it actually printing the quotes as well. As in, it's printing things like this to the console -

"The result is a tie!"

instead of -

The result is a tie!

If I call the function inside a console.log, the quotes don't show though. Is this just some bug with Codecademy editor itself? Or a bug in my code or my understanding of Javascript?


var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();

if (computerChoice < 0.34) {
	computerChoice = "rock";
} 
else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} 
else {
	computerChoice = "scissors";
} 

console.log("Computer: " + computerChoice);
console.log("User: " + userChoice);

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";    
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";  
        }
        else {
            return "paper wins";   
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";    
        }
        else {
            return "scissors wins";
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins";   
        }
        else {
            return "scissors wins";   
        }
    }
}

compare(userChoice, computerChoice);


#2

normally what is returned by a function doesn't show up in the console unless you use console.log, however codecademy echoes the last variable (or in this case the return)

Why codecademy uses qoutation mark when logging the last value to the console? Good question, but normally they won't show, qoutation marks are just used to indicate a string


#3

Thanks. So as I kind of suspected, this is a purely an oddity/bug with codecademy editor and will not happen using Javascript in real world use? Correct?

If this is true, they really should fix this considering people are trying to learn how Javascript will actually behave in a real web environment.


#4

It isn't a bug perse, it can be really useful, since it gives you information which might be useful.

It is very unlikely to have so much console.log statements, since console.log is used for debug, but to explain this concept to someone who has zero programming knowledge.

Choices had to be made. Echoing of the last value is not really a huge difference with real life, beside, as you just determined, you can simple overwrite it.


#5

If it's not a bug, it's most definitely a terrible design decision. I've tried learning other languages and returns were one of the most confusing concepts to me. It seems simple now that I finally get the way it works(everywhere but in codecademy's editor), but for the longest time I thought "But where is it returning TO??" as I thought it was being put into a variable somewhere, but I didn't know what variable. I needed to finally understand that it's only returned when the function is called and then you can put the result/return of that function into a variable if you want. Or you can console.log that function and print the return.

If I hadn't already figured out returns, through alot of trial and error and questions and tutorials, this would make it EVEN MORE confusing than it already was for me. So if it's intended to be this way, that seems even worse than if it was a bug. People are coming here to learn to code in the real world, and they need to understand that a return doesn't actually do anything or go anywhere until you put it there yourself through other code.