8. Functions, return and if / else


#1


Hello! I'm beginner in JavaScript ( and English ).
In order to understand difference between console.log and return, I typed a below code:

// Define quarter here.
var quarter = function (number){
    console.log (number / 4);
    }

if (quarter(12) % 3 === 0 ) {
  console.log("The statement is true");
} else {
  console.log("The statement is false");
}

https://www.codecademy.com/en/courses/javascript-beginner-en-6LzGd/1/3?curriculum_id=506324b3a7dffd00020bf661

The console printed out it.

3
The statement is false
-25

Why did the console print out them?
Especially, It's hard for me to understand why there are "The statement is false" and "-25".

I changed console.log to return and finished this section, however I don't understand the above printing out.

What did JavaScript calculate...?

I thank you for reading it through.


#2

ignore -25, its a test case of the exercise for validation

to understand "The statement is false" run the following code:

var quarter = function (number){
    console.log (number / 4);
    }
x = quarter(12)
console.log(x) // will log undefined

the problem is that your quarter function has no return keyword, so the default (undefined) is returned, which mean if we manually evaluate your if condition:

if (undefined % 3 === 0)

this is false.

your quarter function should return the quarter.


#3

Thank you so much!
I could understand why the console printed out "The statement is false" and an importance of return keyword.


#4