• 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 / Merge two or more arrays together

Merge two or more arrays together

Merge two or more arrays together

Call concat() on the first array, and pass each array to merge with it in as arguments. It returns a new array.

var sandwiches1 = ['turkey', 'tuna', 'blt'];
var sandwiches2 = ['chicken', 'pb&j'];
var allSandwiches = sandwiches1.concat(sandwiches2);

// logs ['turkey', 'tuna', 'blt', 'chicken', 'pb&j']
console.log(allSandwiches);

Source

https://vanillajstoolkit.com/reference/arrays/array-concat/

Miscellaneous

Related Snippets:

  • Check if the user color scheme preference is dark
  • Get an array of key/value pairs from an object
  • Check if an Object Is Empty
  • How To Replace All Occurrences of a String in JavaScript

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