Articles → JAVASCRIPT → Array slice function in JavaScript

Array slice function in JavaScript






Purpose





Syntax


arr_object.slice(); // This will copy the whole array

arr_object.slice(start_index); // This will copy an array from start_index to end of the array

arr_object.slice(start_index, end_index); // This will copy an array from start_index to end_index of the array



Example


<!DOCTYPE html>
<html>
   <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="bootstrap.css" />
      <script>
         var arr = ["e1", "e2", "e3", "e4"];
         
         console.log(arr.slice());
         console.log(arr.slice(1));
         console.log(arr.slice(2, 3));
      </script>
   </head>
   <body>
   </body>
</html>



Output


Picture showing the output of Array slice function in JavaScript

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Saturday, September 4, 2021

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250