Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




slice() - FUNCTION


slice() Function


The slice() Function is used to extract a couple elements from the given Array.


Let us say, we have a Array that contains four names, Mohan, Kriti, Philip and Salim. And we want to extract the elements Kriti and Philip.


Accessing a chunk of elements from the Array using slice() function using positive index


Example :



<html>
<body>  
<script>

	var x = ["Mohan", "Kriti", "Philip", "Salim"]
	var y = x.slice(1,3)
	document.write(y)
    
</script>      
</body>
</html>


Output :



  Kriti,Philip

So, we have the below Array,


var x = ["Mohan", "Kriti", "Philip", "Salim"]
java_Collections


And we want to extract the elements Kriti and Philip from the Array.


var y = x.slice(1,3)
java_Collections


Accessing a chunk of elements from the Array using slice() function using negative index


Example :



<html>
<body>  
<script>

	var x = ["Mohan", "Kriti", "Philip", "Salim"]
	var y = x.slice(-3,-1)
	document.write(y)
    
</script>      
</body>
</html>


Output :



  Kriti,Philip