Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
How do you pass unknown amount of variables into a function in Javascript

I am new to programming, as far as I know, we can pass variables into a function, let's say I can calculate the total price of a product by multiply the quantity and unit price.

const calculatePrice =(quantity,unitPrice)=>{
  let totalPrice=quantity*unitPrice;
  console.log(totalPrice)
  return totalPrice;
}

so I can use the above function to calculate the the total price by doing calculatePrice(2,3.5),so this will give me an answer of 7.

But what if I want to insert two items or three items (unsure amount)? for example,I want to get the the sum of 2*3.5 and 3*4.8 how do I implement this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Functions are first class citizens in javascript, meaning your can use them where you'd use a variable. So, you can add their result as well.

var sumOfPrices = calculatePrice(2,3.5) + calculatePrice(3,4.8);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!