When you erase elements of an array in JavaScript, you use the delete operator or the array's method, splice. What's the difference between these two?
myArray = ['a', 'b', 'c', 'd'];
//delete myArray[1];
//myArray.splice (1, 1);
Please show the difference between delete and splice in this situation.
javascript array
If you write delete first,
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> delete myArray[0]
true
> myArray
[undefined, "b", "c", "d"]
undefined takes over the erased element. And
> myArray = ['a', 'b', 'c', 'd']
["a", "b", "c", "d"]
> myArray.splice(0, 2)
["a", "b"]
> myArray
["c", "d"]
If you use the splice, that part is erased in this way, and then you pull the next part to fill the space.
© 2024 OneMinuteCode. All rights reserved.