📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The Array.concat() method is used to merge two or more arrays. This method does not change the existing arrays but instead returns a new array.
Syntax
var new_array = old_array.concat([value1[, value2[, ...[, valueN]]]])
Arrays and/or values to concatenate into a new array.
Examples
Example 1: Concatenating two arrays
The following code concatenates two arrays:
const letters = ['a', 'b', 'c']; const numbers = [1, 2, 3]; letters.concat(numbers); // result in ['a', 'b', 'c', 1, 2, 3]
Example 2: Concatenating three arrays
The following code concatenates three arrays:
const num1 = [1, 2, 3]; const num2 = [4, 5, 6]; const num3 = [7, 8, 9]; const numbers = num1.concat(num2, num3); console.log(numbers); // results in [1, 2, 3, 4, 5, 6, 7, 8, 9]
Example 3: Concatenating values to an array
The following code concatenates three values to an array:
const letters = ['a', 'b', 'c']; const alphaNumeric = letters.concat(1, [2, 3]); console.log(alphaNumeric); // results in ['a', 'b', 'c', 1, 2, 3]
Example 4: Concatenating nested arrays
const num1 = [[1]]; const num2 = [2, [3]]; const numbers = num1.concat(num2); console.log(numbers); // results in [[1], 2, [3]] // modify the first element of num1 num1[0].push(4); console.log(numbers); // results in [[1, 4], 2, [3]]
Comments
Post a Comment
Leave Comment