-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)
4
  • Also see stackoverflow.com/questions/59792746/… Commented Jul 28, 2022 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 Commented Jul 28, 2022 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] . Commented Jul 28, 2022 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!!!! Commented Jul 28, 2022 at 15:31

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.