Posts

Showing posts from November, 2019

Delete v/s Splice for JavaScript Array

Use "splice" instead of "delete" to delete an item from an array because "Delete" replaces the item with undefined instead of the removing it from the array.


var fruits = ["apple", "banana", "grapes"];
fruits.length; // return 3
delete fruits[1]; // return true
fruits.length; // return 3


Instead

var fruits = ["apple", "banana", "grapes"];
fruits.length; // return 3
fruits.splice(2,1) ;
fruits.length; // return 2