0

I'm learning Javascript ECMA6, but I have a problem, I trying a example from my book and results in Chrome and Safari not equal, my question is my question is whether this will affect developments in the future.

var pizzas = ["Margherita", "Mushroom", "Spinach & Rocket", "Pineapple & Sweetcorn"];
delete pizzas[2]

**Safari**
pizzas; //["Margherita", "Mushroom", 3: "Pineapple & Sweetcorn"]

**Chrome**
pizzas; //(4) ["Margherita", "Mushroom", empty, "Pineapple & Sweetcorn"] 0 : "Margherita" 1 : "Mushroom" 3 : "Pineapple & Sweetcorn" 
length : 4

Version Javascript in Chrome : 6.3.292.46

In Chrome I see empty value in 2 position in Chrome and Safari show a '3' in the next value to detele, this is really important to develop in Javascript?

I think that in Firefox is the same result (other result).

Regards

1 Answers1

2

delete should not be used on arrays. It does not change array length or re-index the array.

Instead use Array#splice()

pizzas.splice(2,1)

will remove 1 element starting at index=2 and reduce the length of the array by one also

See: Deleting array elements in JavaScript - delete vs splice

charlietfl
  • 170,828
  • 13
  • 121
  • 150