Why does an array in javascript continue with the same size even when we use delete?

2

I was running tests on javascript and I noticed that when I use the delete function at an index of array , array continues with the same size.

Example:

a = [1, 2, 3]

delete a[1]

console.log(a.length); // Imprime 3

Why does this happen?

    
asked by anonymous 02.10.2015 / 17:19

1 answer

7

The delete only arrows the index value passed from the array to undefined .

delete a[1];
console.log(a); //[1,undefined,3]

To remove an index from the array you can use the Array.splice

a.splice(1,1); //vai retornar o indice removido [2]
console.log(a); //[1,3]
    
02.10.2015 / 17:25