What are these triple dots (...) in JavaScript ?
Last Updated :
15 Dec, 2023
In JavaScript, there are multiple ways in which we can assign the value of one object to another. Sometimes we do not know the exact number of arguments that are to be assigned. In this case, the triple dots are used.
The triple dots are known as the spread operator, which takes an iterable(array, string, or object) and expands the iterable to individual values. The spread operator is useful as it reduces the amount of code to be written and increases its readability.
The syntax of the spread operator is the same as the rest parameter but the working is different. It was introduced as a feature in ES6 JavaScript
Syntax:
let nameOfVar = [...valueToSpread];
Parameter:
- valueToSpread: The iterable that is to be assigned to the new variable is mentioned here. It can be an Array or a String.
Return Type:
It returns the argument list of the object passed after three dots.
Example 1: In this example, we will concatenate two arrays using the inbuilt concat method and then perform the same task using triple dots syntax.
JavaScript
let baseArr = [1, 2, 3];
let baseArr2 = [4, 5, 6];
// Inbuilt concat method
baseArr = baseArr.concat(baseArr2);
console.log(baseArr);
let spreadArr = ['a', 'b', 'c'];
let spreadArr2 = ['d', 'e', 'f'];
// Concatenating using three dots
spreadArr = [...spreadArr, ...spreadArr2];
console.log(spreadArr);
Output:
[1, 2, 3, 4, 5, 6]
['a', 'b', 'c', 'd', 'e', 'f']
Example 2: In this example, we will copy the contents of an array with the assignment operator and the spread operator:
JavaScript
let baseArr = [1, 2, 3];
let baseArr2 = baseArr;
baseArr2.push(4);
console.log(baseArr2);
console.log(baseArr);
let spreadArr = ['a', 'b', 'c'];
let spreadArr2 = [...spreadArr];
spreadArr2.push('d');
console.log(spreadArr);
console.log(spreadArr2);
Output:
[1, 2, 3, 4]
[1, 2, 3, 4]
['a', 'b', 'c']
['a', 'b', 'c', 'd']
Explanation: We can see that when assigning the array using the assignment operator('=') and modifying the new array, the old array is also modified. This causes a problem as we do not always want to modify the content of the old array but if we use spread operator syntax to assign values and modify the new array, the old array is left unchanged. This happens because a new array is returned by the spread operator instead of the reference of the old array.
Example 3: In this example, we will find the min of an array using the Math.min() method:
JavaScript
let baseArr = [5, 2, 7, 8, 4, 9];
console.log(Math.min(baseArr));
console.log(Math.min(...baseArr));
Output:
NaN
2
Explanation: The math.min() method requires an object list to find the minimum inside the list but when passing an array it gives NaN output. To overcome this problem we used the triple dots format/spread operator. As the spread operator returns a list of objects it is accepted by the method and the minimum element of the array is returned.
Example 4: In this example, we will assign an object to another object using the three dots:
JavaScript
const spreadObj = {
name: 'Ram',
country: 'India',
};
// Cloning previous object
const newObj = { ...spreadObj };
console.log(newObj);
Output:
{name: 'Ram', country: 'India'}
Example 5: In this example, we will use the spread operator in a function call
JavaScript
function add(...objects){
let ans = 0;
for(let i = 0; i < objects.length; i++){
ans += objects[i];
}
console.log(ans);
}
add(1, 2);
add(23, 45, 67, 56);
Output:
3
191
Explanation: We can see that the spread operator is useful when we do not know the number of arguments that are to be passed in a function. Here our add function works for any number of arguments so we do not have to specify multiple functions for a different number of arguments.
NOTE: We should keep in mind that only those objects can be passed using the spread operator which is iterable. For eg., plain objects are not iterable as they lack the Symbol.iterator method.
Example 6: In this example, we will try spreading a plain object.
JavaScript
const plainObj = { name: 'Ram' }; // Spreading non iterable object
const baseArr = [...plainobj];
Output:

Explanation: We can see that when trying to spread a non-iterable object using the three dots an error is thrown.
Similar Reads
What are the builtin strings in JavaScript ? A sequence of letters, special characters, numbers, etc., or a combination of all of them is known as a string. Strings are created by enclosing the string characters within a single quote (') or within double quotes ("). Syntax: var myString = 'Good Morning123!'; // Single quoted string var myStrin
3 min read
What does '...' mean in JavaScript? The '...' (or 3 dot symbol) in JavaScript is known as the Spread operator or Rest operator based on the usage. This syntax is used to expand the iterable into individual elements such as arrays, objects, etc.Syntax for Spread Operatorcosnt a1 = [ 10, 20, 30, 40 ];const a2 = [ ...a1, 50]; // Extracti
4 min read
What do these three dots (...) in React do ? In React the three dots (...) notation is used as the Spread syntax and Rest parameter that has been part of React for a long time when it could be used via transpilation, although, it has been made a part of JavaScript as part of the ES2015 syntax. The Spread syntax is used to deconstruct an array
3 min read
What does +_ operator mean in JavaScript ? Unary Operator: A unary operation contain only one operand. Here, the '+' unary plus operator converts its operand to Number type. While it also acts as an arithmetic operator with two operands which returns an addition result on calculation. JavaScript Identifiers: Javascript Identifiers are used t
2 min read
How to Declare Multiple Variables in JavaScript? JavaScript variables are used as container to store values, and they can be of any data type. You can declare variables using the var, let, or const keywords. JavaScript provides different ways to declare multiple variables either individually or in a single line for efficiency and readability.Decla
2 min read