Why is the code returning NaN?

Hello, everyone!
I am currently studying JavaScript and I have the following question: why does the first code run perfectly and the second return NaN?

First code:

const plantNeedsWater = function(day) {

 return day === 'Wednesday' ?  true :  false;

};

plantNeedsWater('Tuesday');

console.log(plantNeedsWater());

Second code:

const calculateArea = function(width, height) {

  const area = width * height;

  return area;

};

calculateArea(2,3);

console.log(calculateArea());

I really don’t understand.

Hi there, and welcome to the forums!

The function call you are logging to the console is just calculateArea() with no arguments passed to it. A function does not save a calculated value internally or anything like that, it gets the new value every time it is called. Therefore when you run the console.log() part, it is trying to multiply two blank inputs, which results in a NaN (Not a Number) error. You can either change the second function call or save the first to a variable, like below:

const calculateArea = function(width, height) { const area = width * height; return area; }; const area = calculateArea(2,3); console.log(calculateArea(2, 3)); console.log(area);
1 Like

Thank you so much!! I think I understand now.