JavaScript JavaScript Basics Creating Reusable Code with Functions Introducing Functions

Amandeep Pasricha
Amandeep Pasricha
14,928 Points

Do you always have to write a variable for a return value?

Hi, I know this sounds like an obvious question, but how we know when to make a variable or not. I was literally thinking of just putting Math.floor into the function without creating a variable for it. Why would that be wrong?

Do you always need to create a variable?

1 Answer

Steven Parker
Steven Parker
174,023 Points

The "return" statement takes any expression, it can be a variable, but it could also be a formula, literal, function call, etc.

Examples:

    return size * base + 2;  // a calculation
    return price(item);      // call another function
    return "Standard";       // a literal (string in this case)
Steven Parker
Steven Parker
174,023 Points

Amandeep Pasricha — Did this resolve your issue? You can mark a question solved by choosing a "best answer".
And happy coding!