• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Combine two arrays using the spread operator

Combine two arrays using the spread operator

Combine two arrays using the spread operator
const nums1 = [1, 2, 3];
const nums2 = [4, 5, 6];

// LONG FORM
let newArray = nums1.concat(nums2);

// SHORTHAND
newArray = [...nums1, ...nums2];

This syntax can also be used instead of pushing values to an array:

let numbers = [1, 2, 3];

// LONGER FORM
numbers.push(4);
numbers.push(5);

// SHORTHAND
numbers = [...numbers, 4, 5];

Source

https://medium.com/geekculture/20-javascript-snippets-to-code-like-a-pro-86f5fda5598e

Miscellaneous

Related Snippets:

  • Determine whether two values are the same value
  • How To Replace All Occurrences of a String in JavaScript
  • Remove an attribute from an element
  • Strip HTML from a given text

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us