Computer >> Computer tutorials >  >> Programming >> Javascript

How to get the last element in JavaScript array?


To get the last element in JavaScript array, use the slice() method.

Example

You can try to run the following get the last element:

<html>    
   <body>              
      <script>  
         var array = [30,40,50,60];        
         document.write(array.slice(-1)[0]);  
      </script>          
   </body>
</html>