function printAmount( amt ) {
console.log (amt.toFixed(2) );
}
function formatAmount() {
return "$" + amount.toFixed(2);
}
var amount = 99.99;
printAmount (amount * 2); // 199.98
amount = formatAmount();
console.log( amount ); //$99.99
My question is :
amount = formatAmount();
since amount being redefine into function formatAmount() , so console.log(amount) would be function formatAmount(), I understand this part…
BUT WHY the function would return amount(99.99) not the function()(which will cause an error I think)?
in the formatAmount
function there is a return statement. so you get back something when you call the function. you can store whatever is returned as well, as you have done at amount = formatAmount()
the other function printAmount
takes an optional argument amt
and prints to console, but does not return anything
When you refer to formatAmount you are referring to the function. When you put the parentheses, formatAmount() you are telling it to evaluate the function. So,
amount = formatAmount();
isn’t amount equal to formatAmount(), it’s evaluating formatAmount() and putting it’s return value into amount.