Wrapping my mind around spread operator

It’s not so tricky like recursion for me, but this stuff is somewhat contrary to intuition.

For now ended up with this:
NOTE: it’s not a code, it’s kinda cheatsheet.

let someArray = [1, 2, 3]

//Why do i need spread operator?
//I need this stuff from the array for whatever reason, 
//but I don't need it's brackets

//errors below have similar nature
//we are messing with OPERATORS


//Math.sqrt does stuff with one operand, with Number
//spread operator does stuff with one operand, with Array

The spread operator transform an array in a comma-separated list of it’s elements

if you do console.log(...someArray) it’s like you did console.log(1, 2, 3)

same in function arguments, copying array elements in an other array, and other things

The spread operator basically ‘spreads’ the elements inside it out of the list. I understand what it feels like to first understand the spread operator and this example helps me:

const array = [1, 2, 3];
const new_array = [0, ...array, 4, 5, 6];

This example returns [0, 1, 2, 3, 4, 5, 6] and not [0, [1, 2, 3], 4, 5, 6]. I am still no expert myself, but this kind of helps me grasp it a little bit easier.

Hope this helps you a little bit more.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.