Javascript parameter got me confused

const TAX_RATE=0.08;
function calculateFInalPurchaseAmount(amt){
	amt=amt+ amt*TAX_RATE;
    return amt;
    }

var amount= 99.99;

amount = calculateFInalPurchaseAmount(amount);
console.log(amount.toFixed(2));

What does this amt stands for ? Is it an abbreviation of amount?
I understood that the purpose of the function is to calculate the real price of the product, can I swap out all the amt to amount just to avoid confusing myself?

Yes it is. You can give it any name you like.

To elaborate on what Ben is saying…

Yes, it is an abbreviation, and like he said, it could be anything you want.

Understand that amount is the variable outside the function, and amt is a variable that only exists inside the function. In theory, you could have avoided dealing with amt and written the function as:

const TAX_RATE=0.08;
function calculateFInalPurchaseAmount(){
	amount=amount+ amount*TAX_RATE;
    }

var amount= 99.99;

calculateFInalPurchaseAmount();
console.log(amount.toFixed(2));

That would have just changed the variable amount directly. It is not great programming, but it works. But we want a function that will work for any variable, not just amount. So instead we pass a variable to the function. Why call it amt, because it reminds us what it is. We could have called it amount but that might get confusing and might cause us to make a mistake. We could have called it num or price or x or BobUecker. This function would have worked just the same:

const TAX_RATE=0.08;

function calculateFInalPurchaseAmount(peanutButterAndJelly){
	peanutButterAndJelly=peanutButterAndJelly+ peanutButterAndJelly*TAX_RATE;
    return peanutButterAndJelly;
    }

var amount= 99.99;

amount = calculateFInalPurchaseAmount(amount);
console.log(amount.toFixed(2));

Yes, it would have worked, but it would be confusing as heck for a programmer that had to come back and work on this code a few years from now. As I said, calling it amount inside the function could lead to problems and should be avoided, so calling it amt seems like a good compromise. But JavaScript doesn’t give a rat’s behind what you call it.

2 Likes

To answer this question directly, there would be two ways to do that. One way would be the way I did above, where we didn’t pass a variable and just worked directly on the global amount variable directly. Yes, it works, but what if you want to have the function work with different variables?

You could also write the function like this:

const TAX_RATE=0.08;

function calculateFInalPurchaseAmount(amount){
	amount=amount+ amount*TAX_RATE;
    return amount;
    }

var amount= 99.99;

amount = calculateFInalPurchaseAmount(amount);
console.log(amount.toFixed(2));

In this case, JS will assume (correctly) that you want a local variable amount and will keep it separate from the global variable amount - but this could lead to confusion for the programmer. And it is very important to know if it is the local or global amount. If amount is passed to the function so it is a local variable, then anything you do to the variable has no effect on the global amount. The global amount is only changed because of the return statement and the assignment in the second to last line. If you forget whether or not you are dealing with the local or global amount, bad things can happen.

And ultimately it can just lead to confusion. Consider the following program:

var amount = 1;
var amount2 = 3;
var amount3 = 3.14159;

function doubler(amount) {
  console.log('value of local "amount" passed to function = ' + amount);
  amount = amount*2;
  console.log('value of local "amount" after doubling = ' + amount);
  return amount;
}

console.log("before amount is sent -> " + amount);
amount=doubler(amount);
console.log("after amount is sent -> " + amount);

console.log("\nbefore amount2 is sent -> " + amount2);
amount2=doubler(amount2);
console.log("after amount2 is sent -> " + amount2);
console.log("what global amount is after amount2 is sent to function -> " + amount)

console.log("\nbefore amount3 is sent -> " + amount3);
amount3=doubler(amount3);
console.log("after amount3 is sent -> " + amount3);
console.log("what global amount is after amount3 is sent to function -> " + amount);

Run it in codepen and look at the console output and tell me that that doesn’t get a little confusing. Now imagine that the function was complex or that it was a function calling a function calling a function and you needed to keep track of whether amount is meant as the global one or the passed variable.