How to get the last element in JavaScript array?

JavascriptWeb DevelopmentFront End Technology

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

Example

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

Live Demo

<html>    
   <body>              
      <script>  
         var array = [30,40,50,60];        
         document.write(array.slice(-1)[0]);  
      </script>          
   </body>
</html>
raja
Published on 22-Apr-2018 18:15:44
Advertisements