DEV Community

Cover image for ⛓ How to join two arrays in JavaScript
Chandu J S
Chandu J S

Posted on • Edited on • Originally published at chandujs.dev

1

⛓ How to join two arrays in JavaScript

const one = [`aaa`, `bbb`, `ccc`]
const two = [`ddd`, `eee`, `fff`]
Enter fullscreen mode Exit fullscreen mode

You have the above arrays.
How will you merge those into a single array?

You can use concat method.
This method will work in older browsers also.

const megaArray = one.concat(two)
Enter fullscreen mode Exit fullscreen mode

There is a new way to do this.
You can use the spread operator in JavaScript.

const megaArray = [...one, ...two]
Enter fullscreen mode Exit fullscreen mode

Much cleaner!

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more