×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

How to concatenate values to an array in JavaScript?

By IncludeHelp Last updated : January 20, 2024

Problem statement

Given a JavaScript array, you have to concatenate some values in the given array.

Concatenating values to an array

To concatenate values to a JavaScript array, you can use the Array.concat() method. Call the concat() method with the array and pass the values as the parameters, it will return a concatenated array with the values.

JavaScript code to concatenate values to an array

The following example concatenates values to an array arr.

// declaring an array
const arr = ["New Delhi", "Mumbai", "Indore"];

// concatenating array with values
const result = arr.concat(1, 2, 3);

// printing the array
console.log("arr:", arr);
console.log("result:", result);

Output

The output of the above code is:

arr: ['New Delhi', 'Mumbai', 'Indore']
result: ['New Delhi', 'Mumbai', 'Indore', 1, 2, 3]

To understand the above example, you should have the basic knowledge of the following JavaScript topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.