Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Copy Array by Value</title> </head> <body> <script> // Original array var array1 = ["red", "green", "blue"]; // Making a copy var array2 = array1.slice(); // Adding a new element to copied array array2.push("pink"); document.write(JSON.stringify(array2) + "<br>"); // Prints: ['red', 'green', 'blue', 'pink'] document.write(JSON.stringify(array1)); // Prints: ['red', 'green', 'blue'] </script> </body> </html>