💙 Spread and Rest Parameter Operators (...) in JavaScript

Follow this 🧵 for the easiest and never-to-forget type explanation. Thank me later 😝

Happy Weekend 👋

👇👇👇
🤷‍♀️ What is the need? [The Background]

We all know that an array is a collection of elements "kept together" in memory in a contiguous manner so that we can easily access any element using an index.

⇨ As all elements were kept together, how can we break those apart?
🤷‍♀️ Why does an array need to be broken apart?

If elements of an array are going to be "treated separately", you have to take all them out. Let's take an example.

🤷‍♀️ Problem: Print all elements in an array separately.
✅ Solution:

✪ If size is 1.
⇨ console.log(a[0])
✪ If size is 3.
⇨ console.log(a[0], a[1], a[2])

✪ If size is 100.
⇨ We will go mad. Right?

Here we are trying to take elements from array manually on our own. This process is not only lengthy and tedious, error prone as well.
🤷‍♀️ Spread Operator comes to rescue

This is the practical use case of a spread operator. Few languages call this operation "unpacking", but JavaScript calls it "spreading".

🤷‍♀️ What can we spread?

Any iterable (an array, a string etc) or, an object expression (key-value pairs)
🤷‍♀️ What is the syntax?

✪ Use 3 dots (...) before the variable that is to be spread.

✪ Put entire thing
⇨ inside square brackets ([ and ]) (if the variable is an iterable) or,
⇨ inside curly braces ({ and }) (if the variable is an object expression).
✪✪ The 2nd rule is not applicable when an iterable is to be spread inside a function call expression.

Example:

1️⃣
let a = [10, 20, 30]

let b = [...a]
console.log(...a)

2️⃣
let o = {x:10, y:20}
let p = {...o}
🤷‍♀️ Like we broke apart items, can we group them again?

Yes. That's what rest (parameter) operator does.

🤷‍♀️ Why is the confusion, then?

Because both "Spread" and "Rest Parameter" operators look similar. They are identified by 3 dots (...)
🤷‍♀️ Then, how would we know?

✪ In an "assignment expression", there is a LHS and RHS.
✪ RHS is evaluated and, the value is assigned to the LHS.
✪ If we use "..." in RHS, it's a spread operator.
✪ If we use "..." in LHS, it's a rest parameter operator.
🤷‍♀️ How to memorise easily?

R ⇨ Rest Parameter
S ⇨ Spread

✪ Alphabetically R comes before S, so also LHS comes before RHS.

✪ So, the "Rest Parameter Operator" is used in the LHS and the "Spread Operator" is used in the RHS.
🤷‍♀️ The catch

Rest Parameter actually always operates along with destructuring assignment.

Example:

1️⃣
let [a, b, ...c] = [10, 20, 30, 40]

⇨ a = 10, b = 20 and c = [30, 40]

2️⃣
let [...a] = [10, 20]

⇨ a = [10, 20]

🤷‍♀️ What is the restriction?

As the rest parameter operator operates along with destructuring assignment, it can be used only at the end.

Example:

✅ [a, b, ...c] is allowed
❌ [a, ...b, c] is not allowed
🤷‍♀️ Final Example

let name = "Jessica" // string is an iterable

let [f, ...rest] = [...name]

✪ [...name] is at RHS, so it's spreading
✪ "name" is a string which is an iterable. So, it's spread to an array
✪ Hence [...name] is evaluated to ['J', 'e', 's', 's', 'i', 'c', 'a']
✪ ...rest is at LHS, hence it's a rest parameter
✪ [f, ...rest] is an array destructuring expression
✪ "f" is the first element of the array and "rest" is the array containing rest all elements.
✪ "f" is assigned 'J'
✪ "rest" is assigned ['e', 's', 's', 'i', 'c', 'a']
So, we come to the end of this 🧵 How do you feel about this 🧵?

Do you want to know more about me? 👇

And, if you want to visit more contents that I have written here, 👇 🧵 lists all them.

To help others getting access to this content,
♥️ Like this Thread
🔁 RETWEET the first tweet

To never miss any content from me,
✅ Follow @swapnakpanda
🔔 Turn on Notifications

To give feedbacks and suggestions,
💬 Reply

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Swapna Kumar Panda ✨

Swapna Kumar Panda ✨ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @swapnakpanda

14 Nov
📚 Books you must read for

⇨ DSA
⇨ SQL
⇨ JavaScript

Books were and still are the most important medium for our knowledge growth.

Do you love to read books? How about for technical subjects? If no, I would suggest you should.

Here, I will list few of my favourite ones. If you want to add any, feel free.
1️⃣ DSA

1️⃣ Grokking Algorithms
Author: Aditya Bhargava

2️⃣ Introduction to Algorithms
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein

3️⃣ Introduction to Algorithms
Author: Udi Manber

4️⃣ The Algorithm Design Manual
Author: Steven S. Skiena
Read 11 tweets
13 Nov
Introduction to SQL Commands and their Categorisation

Topics to be covered:

0️⃣ Introduction
1️⃣ DDL
2️⃣ DML
3️⃣ DQL
4️⃣ DCL
5️⃣ TCL
6️⃣ Final Words
0️⃣ Introduction

✪ SQL stands for Structured Query Language.

✪ SQL is a computer language for storing, manipulating and retrieving data stored in a relational database.

✪ SQL uses certain commands to carry out the required task.
Read 16 tweets
13 Nov
🤷‍♀️ JavaScript Interview Questions (Series 3)

👉 A detailed coverage on functions

👇
Before proceeding, do check the previous one of this series 👇

Disclaimer:

• The questions covered here are mostly conceptual
• I never claim only these type of questions are/should be asked during interviews
• For interviews, you should have fundamentals strong. And you should be able to provide solutions to practical problems.
Read 18 tweets
12 Nov
👉Interview Questions👈

🕸 Python 🐍 : Series 2

Series: 2️⃣
Level: Beginner
Topics:

1️⃣ Operators
2️⃣ Arithmetic Operators
3️⃣ Logical Operators
4️⃣ Comparison Operators
5️⃣ Assignment Operators
6️⃣ Operators Precedence
7️⃣ Operators Associativity
8️⃣ Operator Overloading
Before proceeding, do check the first one of this series👇

Read 16 tweets
11 Nov
Data Structures & Algorithms (DSA) Interview Questions

Disclaimer:

• The questions covered here are mostly conceptual
• I never claim only these type of questions are/should be asked during interviews
• For interviews, you should have fundamentals strong. And you should be able to provide solutions to practical problems.
Series: 1️⃣
Level: Beginner
Topics:

1️⃣ Algorithms
2️⃣ Data Structures
3️⃣ Array
4️⃣ Array-Algorithms
Read 14 tweets
10 Nov
Hey 👋

I have been working hard to consistently provide you the best possible contents on

🕸 Interview Questions
🕸 DSA
🕸 JavaScript through Infographics
🕸 Python+JS

More to come in future. Don't wanna miss out?

✅ Follow @swapnakpanda
🔔 Turn on Notifications

Details 👇
0️⃣ Who am I?

✪ I am Swapna from India 🇮🇳
✪ I had 10+ years of Experience
✪ I was a Software Architect
✪ I quit job in 2016 for health issues
✪ I am now a Tech Educator
✪ I will soon have my Blog, Portfolio and YT Channel

For what have I posted in Twitter so far, see 👇
1️⃣ Interview Questions

You all love em right? Here are those

✪ DSA
✪ JavaScript
✪ Python
✪ SQL
✪ React
✪ GIT
✪ HTML/CSS
✪ NoSQL
✪ Java
✪ OOPs & Design Patterns
✪ Machine Learning (ML)
✪ Networking

All threads are organised at one place. 👇

twitter.com/i/events/14559…
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Thank you for your support!

Follow Us on Twitter!

:(