Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for The Rest Operator
Sam E. Lawrence
Sam E. Lawrence

Posted on • Edited on

     

The Rest Operator

I had never heard of the Rest Operator until today, but it's a wonderful inverse twist on the Spread Operator. Let's say you need to add up a series of numbers, but you don't know how many numbers your function needs to take in. The Rest Operator is a great way to take every function argument after the first and combine them (the rest) into an array. This is so great because arrays are nice and easy to operate on. Here's an example:

constadd=(param1,...rest)=>{letsum=param1;rest.forEach(arg=>sum+=arg);returnsum;};console.log(add(2,3,4));// 9
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hi, I'm Sam and I'm an advocate for quality software. I'm a Cypress Ambassador and I work as the QA Lead at Pointivo where we're digitizing real-world infrastructure for analysis and maintenance.
  • Location
    ATL
  • Education
    Georgia Tech
  • Pronouns
    He/him/his
  • Work
    QA Lead @ Pointivo
  • Joined

More fromSam E. Lawrence

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp