Why shouldn't I use console.log?


#1

why should not use console.log here?

var isEven = function(number) {
// Your code goes here!
if (number%2 === 0){
return true;}
else if (isNaN(number)){return "that is not a number"}
else{
return false;}
};


#2

This is more like what production code looks like. We pass something to the function, it does something with it and passes back the resulting object.

It also makes the code portable and able to run behind a webpage with no console open. You'll get into the later.

We can still log, but this time we log the call expression,

num = Math.floor(Math.random() * 100 + 1)
console.log(isEven(num));

or store the return value in calling scope,

var result = isEven(num);
console.log(result);

Consider the following demo:

var isEven = function(number) {
    if (number % 2 === 0) {
        return true;
    } else
    if (isNaN(number)) {
        return "that is not a number";
    } else {
      return false;
   }
};

var num = Math.floor(Math.random() * 100 + 1);
// num = 'five';
var result = isEven(num);
if (typeof result === 'boolean') {
    var parity = result ? "Even" : "Odd";
    console.log(num + ", parity: " + parity);
} else {
    console.log(result);
}

This demonstrates how we can work with data that is returned by a function. If we only log the result inside the function, it is lost and cannot be processed outside of the function, like this.