Useful JavaScript Tricks
Looping with indices using for
for([i, e] of ["a", "b", "c"].entries()){
console.log(i, e)
}
Also with forEach
["a", "b", "c"].forEach((e, i) => {
console.log(e, i)
})
Creating an array of length n
const n = 10
let ary = [...Array(n)].map((_, i) => i)
// Or alternatively
ary = Array.from({length: n}).map((_, i)=> i)
Sum of an array
let ary = [1, 2, 3, 4, 5]
ary.reduce((_, v) => _ + v)



-29dc812c795e6f4b2607fd71972b50f0.webp)
-874554613e61e77da9e4727d38fc1856.webp)
-212129d90014b791bd3ed984f6924d7a.webp)
-ff4ba222c43dea2ac8091396141720b3.webp)
-783f089692af11e938b10fb9b942a75b.webp)
-ce5853d1c67cfa52d0dff6bf0e0c2f0e.webp)