DEV Community

Cover image for Mastering JavaScript's Array Slicing: A Modern Approach

Mastering JavaScript's Array Slicing: A Modern Approach

chintanonweb on March 14, 2024

New Array Slice Notation in JavaScript: A Comprehensive Guide Introduction JavaScript, being one of the most widely used programming la...
Collapse
 
miketalbot profile image
Mike Talbot ⭐

It's at TC39 stage 1 isn't it? Not going to be around for a while if that's the case.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Yup, I think you're right there - github.com/tc39/proposal-slice-not...

Doesn't seem to be implemented on any browsers yet either.

I think though, if you use my turboprop and metho-number libraries together you can do stuff like this:

const arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
const arr2 = arr[ 2[to(5)] ]
const arr3 = arr[ 6[to(4)] ]

console.log(arr2)  // c,d,e,f
console.log(arr3)  // g,f,e
Enter fullscreen mode Exit fullscreen mode

You could probably make all the negative number stuff work too, as well as ranges that implicitly start from the beginning or end.
😉

Collapse
 
miketalbot profile image
Mike Talbot ⭐

I absolutely love that stuff you did there. Symantically very easy to understand.