One of the primary uses of the Spread operator is to expand elements of an array or other iterable objects into individual elements:<\/p>\n\n\n
\nconst greeting = 'Hello';\nconst chars = [...greeting];\nconsole.log(chars); \/\/ Outputs: ['H', 'e', 'l', 'l', 'o']\n\n<\/pre><\/div>\n\n\n<\/span>2. Function Arguments from Array:<\/span><\/h3>\n\n\n\nWhen applied in function calls, it can pass the elements of an array as individual arguments to a function.<\/p>\n\n\n\n
\nExample 1:<\/li>\n<\/ul>\n\n\n\nconst numbers = [1, 2, 3];\nconst maxNumber = Math.max(...numbers); \/\/ Same as Math.max(1, 2, 3)\n<\/pre><\/div>\n\n\nExample 2:<\/p>\n\n\n
\nfunction sum(x, y, z) {\n return x + y + z;\n}\n\nconst numbers = [1, 2, 3];\nconsole.log(sum(...numbers)); \/\/ Outputs: 6\n<\/pre><\/div>\n\n\n<\/span>3. Object Cloning:<\/span><\/h3>\n\n\n\nThe Spread operator can be used to copy properties from one object to another, creating a new object.<\/p>\n\n\n\n
Example 1:<\/p>\n\n\n
\nconst obj = { a: 1, b: 2 };\nconst copy = { ...obj }; \/\/ { a: 1, b: 2 }\n<\/pre><\/div>\n\n\nExample 2:<\/p>\n\n\n
\nconst user = { name: 'John', age: 30 };\nconst userWithId = { ...user, id: '123' };\nconsole.log(userWithId); \/\/ Outputs: { name: 'John', age: 30, id: '123' }\n<\/pre><\/div>\n\n\n<\/span>4. Merging Arrays:<\/span><\/h3>\n\n\n\nMerges properties from multiple objects into a new object.<\/p>\n\n\n\n
Example 1:<\/p>\n\n\n
\nconst defaults = { a: 1, b: 2 };\nconst options = { b: 3, c: 4 };\nconst settings = { ...defaults, ...options }; \/\/ { a: 1, b: 3, c: 4 }\n<\/pre><\/div>\n\n\nExample 2:<\/p>\n\n\n
\nconst fruits = ['apple', 'banana'];\nconst moreFruits = ['orange', 'grape'];\n\nconst allFruits = [...fruits, ...moreFruits];\nconsole.log(allFruits); \/\/ Outputs: ['apple', 'banana', 'orange', 'grape']\n<\/pre><\/div>\n\n\n<\/span>5. Array Concatenation:<\/span><\/h3>\n\n\n\nCombines two or more arrays:<\/p>\n\n\n
\nconst arr1 = [1, 2];\nconst arr2 = [3, 4];\nconst combined = [...arr1, ...arr2]; \/\/ [1, 2, 3, 4]\n<\/pre><\/div>\n\n\n<\/span>Second: The Rest Operator<\/span><\/h2>\n\n\n\nWhile it looks identical to the Spread operator, the Rest operator is used to condense multiple elements into an array.<\/p>\n\n\n\n
<\/span>1. Function Parameters into an Array:<\/span><\/h3>\n\n\n\nWhen defining function parameters, the Rest operator allows us to gather any number of arguments into an array.<\/p>\n\n\n\n
Example 1:<\/p>\n\n\n
\nfunction logArguments(...args) {\n console.log(args);\n}\nlogArguments(1, 2, 3); \/\/ [1, 2, 3]\n<\/pre><\/div>\n\n\nExample 2:<\/p>\n\n\n
\nfunction multiply(multiplier, ...theArgs) {\n return theArgs.map(element => multiplier * element);\n}\n\nconst result = multiply(2, 1, 2, 3);\nconsole.log(result); \/\/ Outputs: [2, 4, 6]\n<\/pre><\/div>\n\n\n<\/span>2. Destructuring Arrays Elements:<\/span><\/h3>\n\n\n\nThe Rest operator is handy at capturing all remaining elements in an array during destructuring.<\/p>\n\n\n\n
Example 1:<\/p>\n\n\n
\nconst [first, ...rest] = [1, 2, 3, 4]; \/\/ first = 1, rest = [2, 3, 4]\n<\/pre><\/div>\n\n\nExample 2:<\/p>\n\n\n
\nconst [first, ...restOfItems] = [10, 20, 30, 40];\nconsole.log(first); \/\/ Outputs: 10\nconsole.log(restOfItems); \/\/ Outputs: [20, 30, 40]\n<\/pre><\/div>\n\n\n<\/span>3. Destructuring Object Properties:<\/span><\/h3>\n\n\n\nIt can also be used in collecting all remaining own enumerable property keys that are not already picked off by the destructuring pattern.<\/p>\n\n\n\n
Example 1:<\/p>\n\n\n
\nconst { a, ...rest } = { a: 1, b: 2, c: 3 }; \/\/ rest = { b: 2, c: 3 }\n<\/pre><\/div>\n\n\nExample 2:<\/p>\n\n\n
\nconst { firstItem, ...restOfItems } = { firstItem: 'apple', secondItem: 'banana', thirdItem: 'cherry' };\nconsole.log(firstItem); \/\/ Outputs: 'apple'\nconsole.log(restOfItems); \/\/ Outputs: { secondItem: 'banana', thirdItem: 'cherry' }\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"In JavaScript, the three dots (…) serve as two distinct operators depending on their context. These operators add to the flexibility and expressiveness of JavaScript, making the manipulation of arrays, objects, and function arguments more succinct and intuitive, they both utilize the same ellipsis syntax, but their functions are quite different. Read What Does \u2018$\u2019 […]<\/p>\n","protected":false},"author":1,"featured_media":1120,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[7],"tags":[],"blocksy_meta":[],"yoast_head":"\n
What Do The Three Dots (...) Mean in JavaScript? - SEOStudio<\/title>\n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n\t \n\t \n\t \n