Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between slice and delete method in javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 95
    Comment on it

    splice() : it is used to delete an item from an array that effects array length.
    delete() : it replaces the item with undefined instead of the removing it from the array. It means that it does not effect the array length.

    note:Instead of using delete we should use the splice method to delete the element from an array.

    Example:

    var objects = [10, 54 ,'b' , 3 , 544 , 'fruit' , 8852 , 'Dae' , 2334 , 129 ]; 
    objects.length; // return 10 
    delete items[3]; // return true 
    objects.length; // return 10 
    
    /* items will be equal to [10, 54 ,'b' , undefined  1 , 544 , 'fruit' , 8852 , 'Dae' , 2334 , 129 ]   */
    

    Use

    var objects = [10, 54 ,'b' , 3 , 544 , 'fruit' , 8852 , 'Dae' , 2334 , 129 ]; 
    objects.length; // return 10 
    objects.splice(3,1) ; 
    objects.length; // return 9
    /* items will be equal to [10, 54 ,'b' , 544 , 'fruit' , 8852 , 'Dae' , 2334 , 129 ];   */
    

    The delete method should be used to delete an object property.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: