-1

I am trying to learn javascript but having trouble in the spread and rest operators. Can't understand whats happening here how dose this take in the taxRate parameter like a singel number when we spred the itemsBought parameter

function addTaxToPrices (taxRate, ...itemsBought)
    {
        return(itemsBought.map(item => item*taxRate));
    }
    let ShoppingCart=addTaxToPrices(1.1,46,89,35,79);
    console.log(ShoppingCart)
  • Also see https://stackoverflow.com/questions/59792746/how-does-the-spread-rest-operator-in-js-work – Suraj Rao Jul 28 '22 at 15:16
  • That's just the syntax, whenever you use the rest operator it captures all the extra arguments not captured by a named paramater – Samathingamajig Jul 28 '22 at 15:16
  • First argument becomes the taxRate, so the value 1.1 . The rest operator will collect all the arguments past the first one into an array. So itemsBought becomes [46,89,35,79] . – Shilly Jul 28 '22 at 15:17
  • @Shilly thanks i get now So the rest operator is just a spread operator used to at time of declaration of an array that we don't know the size-of ?Thanks a bunch!!!! – Pranshu Sati Jul 28 '22 at 15:31

1 Answers1

0

for my understanding it is nearly the same than if you do:

function addTaxToPrices (taxRate, itemsBought)
    {
        return(itemsBought.map(item => item*taxRate));
    }
itemsBought = [46,89,35,79]
let ShoppingCart=addTaxToPrices(1.1,itemsBought);
console.log(ShoppingCart)
shortQuestion
  • 473
  • 2
  • 16