DEV Community

Discussion on: JavaScript object destructuring usages you must know

Collapse
 
jonrandy profile image
Jon Randy 🎖️
const str = 'Kumquat'
const {length, [length-1]:last} = str

console.log(length) // 7
console.log(last) // 't'
Enter fullscreen mode Exit fullscreen mode
Collapse
 
atapas profile image
Tapas Adhikary

Mindblowing. Thanks, John for sharing.

Collapse
 
kazuyastefan profile image
Stefan Grbic

I was playing a bit with at(), but it's not supported in all browsers.

const str = 'Kumquat'
const {length, last = str.split('').at(-1) } = str;

console.log(length) // 7
console.log(last) // 't' 
Enter fullscreen mode Exit fullscreen mode