Skip to content

JavaScript Spread syntax | Example with iterables objects

  • by

JavaScript Spread syntax is an operator, used to expand or spread an iterable or an array. It can provide a function call with an array (or any other iterable) where 0 or more arguments were expected.

JavaScript Spread syntax example

Simple example code print array values.

<!DOCTYPE html>
<html>
<body>
  <script type="text/javascript">
    const arrValue = ['A', 'B', 'C', 'D'];

    console.log(arrValue);   
    console.log(...arrValue);
  </script>

</body>
</html>

Output:

JavaScript Spread syntax

Other uses of JavaScript Spread syntax

You can also use the spread syntax ... to copy the items into a single array. For example,

const arr1 = ['one', 'two'];
const arr2 = [...arr1, 'three', 'four', 'five'];

console.log(arr2); 

Output: [“one”, “two”, “three”, “four”, “five”]

Do comment if you have any doubts or suggestions on this JS basic topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *