Slice in arrays :- Genius student


  • The slice() method used to return the selected elements in an array, as a new array object.
  • It does not change the original array.
  • The slice() method cannot take more arguments it ca take only 2 arguments.

Code:-


<!DOCTYPE html>
<html>

<head>
 <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

 <h1>slice() in Array</h1>

 <input type="button" onclick="fun1();" value="slice(1)">

 <input type="button" onclick="fun2();" value="slice(2,4)">
 <script>
  var fruits = ["Jan", "Feb", "Mar", "Apr", "May"];

  function fun1()
  {
   alert("Actual : " + fruits);
   alert(fruits.slice(1)); //return 2 to 5th value  (Not removed)
  }

  function fun2()
  {
   alert("Actual : " + fruits);
   alert(fruits.slice(2, 4)); //return 3 to 4th value
  }
 </script>

</body>

</html>
Slice in arrays :- Genius student Slice in arrays :- Genius student Reviewed by Network security on October 06, 2019 Rating: 5

No comments:

Useful Information

Powered by Blogger.