/* Function To Merge two Arrays Using For Loops @param arr1, arr2 */ const mergeArr = (arr1, arr2) => { const arr3 = []; debugger; if (arr1.length > 0 && arr2.length > 0) { for (let i = 0; i < arr1.length; i++) { arr3[i] = arr1[i]; } for (let i = 0; i < arr2.length; i++) { arr3[arr1.length + i] = arr2[i]; } console.log(arr3); return arr3; } else { alert("Both Arrays can not be empty"); } }; const arr1 = [1, 2, 3, 4, 5]; const arr2 = [6, 7, 8, 9, 10]; mergeArr(arr1, arr2); // With help shallow copy we can merge multiple arrays const arr = [...arr1, ...arr2]; console.log(arr);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter