Three Dots of JS (Spread vs Rest Operator {...})
Credits : Medium.com

Three Dots of JS (Spread vs Rest Operator {...})

Every programming language comes with its own distinct and comprehensive library for implementing various features. For instance, in some languages, we can directly assign one iterable, such as an array, vector, or string, to another newly created iterable. However, JavaScript doesn’t allow this as straightforwardly. JavaScript addresses this through the concept of destructuring and introduces special operators like the spread and rest operators to handle these tasks. We'll first explore the spread operator and then compare it with the rest operator.

Spread Operator:

The spread operator is used to expand elements from an iterable (such as an array, string, or JavaScript object) into individual elements or variables. It is represented by three dots {...}.

Credits : Interview Happy

The spread operator is not confined to just this use case; in fact, it offers several fascinating features that we will explore one by one.

Copying Arrays:

As mentioned earlier, if we have an array and create an empty array or object in JavaScript, we cannot simply use the assignment operator to copy the contents of the existing array into the new one. Doing so would result in both arrays pointing to the same memory location, meaning any modifications to the new array would also affect the original one. To avoid this, we use the spread operator, which solves the problem. The spread operator is applied to the original array on the right side of the assignment operator.

Credits : Interview Happy

Merging Arrays:

The spread operator can also be used to merge two arrays into a new array. This allows you to combine the contents of multiple arrays effortlessly. The syntax for this is simple and intuitive:



Credits : Interview Happy

As Function Parameters:

The spread operator can also be used when calling functions. By passing an array as an argument, preceded by the spread operator, the array is expanded into individual elements within the function. This allows the function's parameters to accept each element of the array as separate arguments.


Credits : Interview Happy

React & Spread Operator :

In React, we frequently use the spread operator to save time and make the code more concise. When working with multiple components, if we need to modify some functionality, we can simply update a specific prop without changing the entire component. However, while this approach can streamline development, it has a downside—it often makes the code less declarative and harder to maintain. The use of the spread operator can make it challenging to identify which props a component is using, making debugging more time-consuming for developers.

Rest Operator:

The rest operator is essentially the reverse of the spread operator, using the same {...} syntax. Instead of expanding an iterable, the rest operator combines multiple individual elements or variables into a single iterable, such as an array or object. When working with objects, the rest operator is often used with destructuring syntax to group remaining elements into an object. In this case, the rest operator appears on the left side of the assignment operator.

For example:

Credits : Interview Happy

In above example , a would be 1 b would be 2 and rest array would stored in "others"

As Function Arguments:

The rest operator can also be used in function calls, where it allows an iterable to be created from individual arguments. By using the rest operator in the function parameters, all constants or variables passed to the function are combined into a single iterable, such as an array. However, unlike the spread operator, which can be used at any position in the function arguments, the rest operator must always be placed at the end of the parameter list.


Credits : Interview Happy

In summary, the spread and rest operators in JavaScript offer powerful ways to manage iterables and function arguments. The spread operator expands iterables into individual elements, while the rest operator combines elements into a single iterable. So, Keep exploring and learning, as mastering these concepts and their applications will enhance your skills and open up new possibilities in your projects.

要查看或添加评论,请登录

M. Shahmeer Zaidi的更多文章

  • Event Bubbling & Event Delegation in JS

    Event Bubbling & Event Delegation in JS

    In JavaScript, events in the DOM have a default behavior known as "event bubbling." This concept is analogous to water…

    1 条评论
  • Before::& After:: (The Power Divolution by CSS)

    Before::& After:: (The Power Divolution by CSS)

    Entering the world of frontend development, newbies often find it surprising that Bootstrap and Tailwind CSS offer…

    2 条评论

社区洞察

其他会员也浏览了