Three dots can make a huge difference in a sentence. In JavaScript, this is not the exception. In JavaScript, the spread operator is identified by using three dots. This syntax is very powerful because it allows you to perform plenty of different tasks in a very simple and easy way.

Let’s go over some examples of how to use this syntax on JavaScript.

Passing arrays as arguments

The spread operator lets you grab an array, and decompose it in such a way that now you can have each one of the values out of it and can be passed as arguments to a function.

Let’s start by assuming you have the following code. A function that takes in three arguments and logs them separated by commas and an array with three elements.

const fun = (a, b, c) => console.log(`${a}, ${b}, ${c}`);
const arr = [0, 1, 2];

You could do this in a simple but not ideal way. This is, to take each one of the elements out of the array by their indexes and passing them as arguments.

const fun = (a, b, c) => console.log(`${a}, ${b}, ${c}`);
const arr = [0, 1, 2];

fun(arr[0], arr[1], arr[2]);

#javascript-development #typescript #javascript #arrays #software-development

JavaScript spread operator: To infinity and beyond!
1.45 GEEK