R
Reactiflux

venus – 15-09 Dec 10

venus – 15-09 Dec 10

Vvenus12/10/2022
Can i write this condition in shorter version? I want to get last index of array arr and if the array is emty, I want to get 0 (so basically add -1 to arr.length if arr.length is not 0)
const index = !!arr.length ? arr.length - 1 : 0;
const index = !!arr.length ? arr.length - 1 : 0;
SScriptyChris12/10/2022
Probably like this
const index = arr.length ? arr.at(-1) : 0;
const index = arr.length ? arr.at(-1) : 0;
(or maybe with binary shift magic 🤔 ) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT
UUUnknown User12/11/2022
Message Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

venus – 15-09 Dec 10

Join Server