DEV Community

Discussion on: 5 useful javascript tricks for begginers.

Collapse
 
nibelune profile image
Benoît Schiex • Edited

last_Item = arr.slice(-1) would not return the last item but a new array containing the last item.

you can do :

const lastItem = arr[arr.length-1]
const lastItem = arr.slice(-1)[0]
const lastItem = arr.slice(-1).pop()
const [lastItem] = arr.slice(-1)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
developeratul profile image
Info Comment hidden by post author - thread only visible in this permalink
Minhazur Rahman Ratul

wow nice. !

Some comments have been hidden by the post's author - find out more