Why do I get undefined?

I don;t understand why I get undefined when I log totalCost to the console.
Here is the code:
function monitorCount(rows, columns) {
return rows * columns;
}

function costOfMonitors(rows, columns){
return monitorCount(rows, columns)*200;
}
const totalCost = console.log(costOfMonitors(5,4));
console.log(totalCost);

The result is:
4000
undefined

We would not assign console.log since it returns undefined.

let totalCost = costOfMonitors(5, 4);
1 Like

I had the same issue with my code, then I replaced console.log with return and I am not getting the error anymore.
why is this? please explain.

my code ( i know it’s messy ) and I replaced log statements with return in my if statements, if I use console.log it will print the output but I’ll also get undefined with it. :

const getSleepHours = day => {

switch (day){

case 'monday':

  return 8;

  break;

case 'tuesday': 

  return 5;

  break;

case 'wednesday':

  return 6;

  break;

case 'thursday':

  return 7;

case 'friday':

  return 5;

  break;

case 'saturday':

  return 4;

  break;

case 'sunday':

  return 10;

  break;

}

}

const getActualSleepHours = () =>{

const sum = (getSleepHours(‘monday’) + getSleepHours(‘tuesday’) + getSleepHours(‘wednesday’) + getSleepHours(‘thursday’) + getSleepHours(‘friday’) + getSleepHours(‘saturday’) + getSleepHours(‘sunday’));

return sum;

}

const getIdealSleepHours = () => {

const idealHours = 7;

return idealHours * 7;

}

const calculateSleepDebt = () => {

const debt = (getIdealSleepHours() - getActualSleepHours());

if (debt === 0) {

return ' the user got the perfect amount of sleep.';

}

else if (debt < 0) {

  return 'the user got more sleep than needed.';

  }

else {

  return 'the user should get some rest.';

}

}

console.log(calculateSleepDebt());

// console.log(getIdealSleepHours());

// console.log(getActualSleepHours());

// console.log(getSleepHours(‘tuesday’));

You were getting an additional undefined for the same reason as the original post. As @mtf mentioned, console.log will return undefined. Actually it returns nothing, but a JavaScript function will return undefined by default even if you didn’t write a return statement.

You can confirm this by checking the console.log function on MDN:

And here are some examples.

// A function that writes a greeting to the console.
function greet(person) {
	console.log('hello ' + person);
}

console.log(greet('RyanSUP')); // We are saying log the return value of greet('RyanSUP')
// greet() will return undefined by default, so 'undefined' gets passed into console.log
// Logs:
// hello RyanSUP
// undefined

greet('RyanSUP');
// logs:
// hello RyanSUP

// Return a greeting
function returnGreeting(person) {
	return 'hello ' + person;
}

console.log(returnGreeting('RyanSUP')); // returnGreeting returns a string, so we can log it and get the desired result.
// Logs:
// hello RyanSUP

// We can varify the return type by using typeof:
console.log(typeof greet('RyanSUP')); // Logs: undefined
console.log(typeof returnGreeting('RyanSUP')); // Logs: string
1 Like