3 Uses of the Three Dots in JavaScript
Make your JavaScript code much more elegant
If you read any modern JavaScript programs, you probably will meet one thing many times — the ellipsis. It is just three dots (…) which was introduced by ES6. Looks very simple and unremarkable? However, it changed JavaScript a lot.
Let’s feel it by a simple example:
Now, we have a string called author
as following:
let author = "Yang"
How can we convert this string to an array including all the characters?
In C or C++, we need to define an array at first and then use a for loop to iterate all characters and add them into the array one by one.
In JavaScript, we just need one line of code:
let authorChar = [...author]
The ellipsis makes JavaScript much more elegant. Isn’t it?
In fact, the above example only shows the tip of the iceberg. This article will dive into 3 major uses of the three dots in JavaScript and show how they make JavaScript great.
1. Destructuring Assignment in JavaScript
This feature makes the assignment operation in JavaScript more elegant.