I found the following code and I don't know what is the difference between A and B:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
A
fruits.splice(2,0,["Lemon", "Kiwi"]);
B
fruits.splice(...[2,0].concat(["Lemon", "Kiwi"]));
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var A = fruits.splice(2, 0, ["Lemon", "Kiwi"]);
var B = fruits.splice(...[2, 0].concat(["Lemon", "Kiwi"]));
console.log(A)
console.log(B)
Without using spread, B is:
The
concat
is a bit confusing, it could also have been written as:Or:
All other answers correctly describe
splice
method and spread operator behavior, but none tries to correct your misunderstanding about the result.What you are actually seeing, is the return value of
splice
method, which is an array containing the removed elements, which in both your method calls, are none. That's why you get an empty array as the result.To see the behavior of calling
splice
method, you need to log thefruits
array after each call (not the return value of the method), which in your case is not so much useful, because your expectations about thefruits
array does not hold in the second call.A treats
["Lemon", "Kiwi"]
as one item and inserts it in given indexB concats
[2,0]
and["Lemon", "Kiwi"]
and then passes them to splice as comma seperated arguments likewhich modify the array like below
First of all you need to understand how splice works
it takes start(starting index from zero), number of elements to be deleted, and rest any arguments will be added at that starting index.
Now you are clear with splice, so let go step by step for clearer understanding of those statments.
The following statement
after concatenation becomes
after spread it becomes
then splice will take fruits from index 2 and delete nothing(as given zero) and add rest of the arguments ie., "Lemon" and "Kiwi"
So You get
["Banana", "Orange", "Lemon", "Kiwi", "Apple", "Mango"]
Where as in
the splice will take fruits from index 2 and delete nothing(again as given zero) and add rest of the arguments i.e, "["Lemon", "Kiwi"]"
So you get
["Banana", "Orange", ["Lemon", "Kiwi"], "Apple", "Mango"]
I hope it helps.
As per the doc from function signature:
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
In B:
Because
[2,0].concat(["Lemon", "Kiwi"])
means[2,0,"Lemon", "Kiwi"]
.So
fruits.splice(...[2,0,"Lemon", "Kiwi"]);
becomesfruits.splice(2,0,"Lemon", "Kiwi");
using the spread operator(...
).Above code you are saying add
"Lemon"
,"Kiwi"
, from index2
witho deleting 0 items.In this case
2
isstart
index,deleteCount
is0
, anditem1
is"Lemon"
,item2
is"Kiwi"
.Now in A:
You are saying add
["Lemon", "Kiwi"]
, from index2
with deleting 0 items. In this case2
isstart
index,deleteCount
is0
, anditem1
is["Lemon", "Kiwi"]
.First of all, Statement A & Statement B will generate different results.
In
Statement A
, you are inserting an array (["Lemon", "Kiwi"]
) as an array element at position 2 while removing 0 items. So, you are inserting a string array in another string array at position 2.However,
Statement B
is much more interesting. To, understand it fully, first log out it's core portion like this:As you can see it generates,
2 0 Lemon Kiwi
. Then it is passed as parameter tofruits.splice(..here..)
. According toarray#splice
it will enter two strings (Lemon & Kiwi) at position 2, while removing 0 elements.NOTE:
array#splice
updates the original array.Statement A
inserts anarray
(IE["Lemon", "Kiwi"]
) in parent string array whereas,Statement B
inserts two strings (IE'Lemon', 'Kiwi'
) in parent string array.