miomate
miomate

Reputation: 45

Local variable instead parameter in function, better understanding of functions

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.

Upvotes: 0

Views: 155

Answers (3)

Lae Kettavong
Lae Kettavong

Reputation: 399

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));

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370809

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);

Upvotes: 1

connexo
connexo

Reputation: 56753

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.

Upvotes: 0

Related Questions