It's because Array.splice deletes and inserts items in array, then returns any items that were deleted. So if we don't delete any items, return value of Array.splice(n, 0, ...items) will always be empty [].
In your example, you just need to log output as well as result of splice call:
And if you're wondering why you see individual characters from elements variable; it's because you're using rest parameter, which will spread iterable contents into function args. In case of string this is the individual characters of its value.
If this isn't what you want, and instead you just want to insert string value, you just need to remove rest operator:
This is also the reason why we spread arr into a new output array, before calling splice, because it mutates the array it's called on. When designing our implementation, it's better to avoid mutating input and let the consumer decide if they want to mutate.
For example, consumer "mutates" by assigning output of someFn to previously declared variable arr:
I think the fact that splice is mutative leads a lot of people to avoid it. However, it's really useful! Here's some of the things you can do with it...
Thank You for long explanantion, everything make sense now. I havent used these methods very often so forgot there nature and what it does and for some reason i havent noticed that it mutates the original array it self in mdn documentation. Stupid me...
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hey i was just checking your code out, it works but why splice method for me returns an empty array but works for you.
It's because
Array.splice
deletes and inserts items in array, then returns any items that were deleted. So if we don't delete any items, return value ofArray.splice(n, 0, ...items)
will always be empty[]
.In your example, you just need to log
output
as well as result ofsplice
call:And if you're wondering why you see individual characters from
elements
variable; it's because you're using rest parameter, which will spread iterable contents into function args. In case ofstring
this is the individual characters of its value.If this isn't what you want, and instead you just want to insert
string
value, you just need to remove rest operator:This is also the reason why we spread
arr
into a newoutput
array, before callingsplice
, because it mutates the array it's called on. When designing our implementation, it's better to avoid mutating input and let the consumer decide if they want to mutate.For example, consumer "mutates" by assigning output of
someFn
to previously declared variablearr
:I think the fact that
splice
is mutative leads a lot of people to avoid it. However, it's really useful! Here's some of the things you can do with it...So to summarise:
Array.splice
mutates its array.Array.splice
returns items that were deleted from its array.Array.splice
syntax is:Hope this helps explain it more ;)
Thank You for long explanantion, everything make sense now. I havent used these methods very often so forgot there nature and what it does and for some reason i havent noticed that it mutates the original array it self in mdn documentation. Stupid me...