How to get the last element in an array with Node.js

3 Answers

0 votes
const arr = [10, 20, 30, 40, 50, 60, 70];
 
console.log(arr.at(-1));
 
 
 
 
/*
run:
 
70
 
*/

 



answered Feb 10, 2024 by avibootz
0 votes
const arr = ['javascript', 'c++', 'php', 'python', "node.js"];
    
const last = arr[arr.length - 1];
  
console.log(last);

 
    
        
        
/*
run:
        
node.js
        
*/

 



answered Feb 10, 2024 by avibootz
0 votes
const arr = ['javascript', 'c++', 'php', 'python', "node.js"];
    
const last = arr.slice(-1);
   
console.log(last);

 
    
        
        
/*
run:
        
[ 'node.js' ]
        
*/

 



answered Feb 10, 2024 by avibootz
...