0

Had troubles choosing title. I am learning js and looking for place to ask questions, most of the time they are simple. Is stackoverflow a good place for that or can you recommend another place (irc or forum)?

Begann to work with functions in js. These lines are given:

function calculateTax(amount){
  let result = amount * 0.08;
  return result;
  }
  let tax = calculateTax(100);
  console.log(tax);

I ask my self why function needs a local variable "result", why couldn't the parameter be used:

function calculateTax(amount){
  amount * 0.08;
  return amount;
  }
  let tax = calculateTax(100);
  console.log(tax);

My guess is because the parameter is a placeholder or is a variable needed to safe the multiplication and the 0.08? I think I read that parameters are variables to, is this correct? Is the 100 in the function a parameter too?

I think I waste to much time on such things. My problem is, I am to curious. Thank you for your time.

2
  • You can use function calculateTax(amount){ return amount * 0.08; } Commented Sep 21, 2018 at 6:42
  • You declare a function, say it has a parameter called amount… it’s nice for readability if amount always refers to the parameter instead of switching to something else partway through the function. In this particular case, as people have gotten at, result doesn’t add much value as a new name and the better way to write it is a direct return amount * 0.08. Commented Sep 21, 2018 at 6:55

3 Answers 3

1

Assuming that amount is a number, then either method works. That said, reassigning a paraneter when one doesn't have to is generally considered to be a bit of a code smell - see see no-param-reassign.

In your original code, both amount and result are local variables. (yes, parameters are variables too)

Of course, an alternative to declaring a new variable or reassigning amount is to simply return the result of the calculation immediately:

function calculateTax(amount) {
  return amount * 0.08;
}
let tax = calculateTax(100);
console.log(tax);

Primitives are immutable, so changing amount inside the function won't cause any side-effects. If amount wasn't a primitive, then you would have to worry about changing it. For example, if it was an object with a value property, then if you changed that property inside the function, the object that was passed in will be mutated. To illustrate:

function calculateTax(obj) {
  obj.amount *= 0.08;
  return obj.amount;
}

const item = { amount: 100 };
let tax = calculateTax(item);
console.log(tax);

// item was mutated inside the function:
console.log(item);

Sign up to request clarification or add additional context in comments.

2 Comments

I’m not sure what the best wording is, but “primitives are immutable, so changing amount inside the function won’t cause any side-effects” mixes two unrelated types of “changing”. Statements like that are the source of a lot of confusion about what pass-by-reference is, for example.
Everything in JavaScript is pass-by-value, it's just that non-primitives themselves are references, so the reference is passed-by-value. That statement makes more sense if you're familiar with C/C++ pointer handling.
0

The reason for introducing a variable is that in many cases what you want a function to do with a parameter isn't quite as simple as in your example. For example another function might take the parameter, send it wrapped in an object, stringified to JSON to a REST service, await the response from that server, and do some calculations on that, and in the end, return it.

In your case I'd even advocate omitting the extra creation of a variable because what the function does is so trivial.

Comments

0

Glad you're learning JavaScript. For more terse syntax (and up your game), use ES6 arrow function notation as follows

const calculateTax = (amount) => amount * 0.08;
console.log(calculateTax(100));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.