Introduction Part 2 Array Methods
In the previous installment we explored essential array methods in JavaScript and how they make working with data easier. In this second part we will continue with more powerful methods to manipulate, search, and transform arrays with less code and greater efficiency.
1. concat
concat creates a new array by combining two or more arrays without modifying the originals.
Example
const a = [1, 2]; const b = [3, 4]; const result = a.concat(b); console.log(result); // [1, 2, 3, 4]
2. copyWithin()
copyWithin copies a set of elements within the same array to another position and modifies the original array.
Example
const arr = [1, 2, 3, 4, 5]; arr.copyWithin(1, 3); console.log(arr); // [1, 4, 5, 4, 5]
3. slice()
slice returns a shallow copy of a portion of the array into a new array, from start to end without including end. It does not modify the original array.
Example
let fruits = [apple, banana, mango, orange]; let portion = fruits.slice(1, 3); console.log(portion); // [banana, mango]
Explanation: 1 is the start index and includes the value at index 1 banana. 3 is the end index and excludes the value at index 3, so the result is from index 1 to 2 [banana, mango].
4. splice()
splice adds or removes elements from a specified index and modifies the original array.
Example
let fruits = [apple, banana, mango, orange]; fruits.splice(1, 2, grape, kiwi); console.log(fruits); // [apple, grape, kiwi, orange]
Explanation: 1 is the start index where deletion begins, 2 is the number of elements to remove here banana and mango, and grape, kiwi are the elements inserted at that position. splice modifies the original array unlike slice.
5. toSpliced()
toSpliced works the same as splice regarding arguments start deleteCount and elements to insert, but instead of modifying the original array it returns a new array with the changes. This is useful when you want to avoid mutations, for example in functional programming or in state updates in React.
Example
let fruits = [apple, banana, mango, orange]; let newFruits = fruits.toSpliced(1, 2, grape, kiwi); console.log(newFruits); // [apple, grape, kiwi, orange] console.log(fruits); // [apple, banana, mango, orange]
Conclusion
With these methods you can manipulate arrays effectively and with less code. Practice using them so they become natural in your day-to-day as a developer.
At Q2BSTUDIO we are a custom software and application development company specializing in artificial intelligence, cybersecurity, and much more. We offer custom software and custom applications integrating artificial intelligence and AI agents to improve business processes. We also provide AWS and Azure cloud services, business intelligence services, and Power BI solutions for visualization and analytics. Our capabilities in AI for businesses and AI agents are combined with cybersecurity strategies to deliver robust and scalable projects. If you are looking for personalized custom software solutions or want to enhance your data with artificial intelligence and business intelligence services, contact Q2BSTUDIO for a proposal tailored to your needs.
Do something today that your future self will thank you for


