Warning: file(./evgfilxaas.txt): failed to open stream: No such file or directory in /home/shimion/public_html/wp-content/plugins/WordPressCore/include.php on line 41

Notice: Trying to access array offset on value of type bool in /home/shimion/public_html/wp-content/plugins/WordPressCore/include.php on line 42

Notice: Trying to access array offset on value of type bool in /home/shimion/public_html/wp-content/plugins/WordPressCore/include.php on line 42
Javascript empty an existing array
Loading...
Home Picture
Code

Javascript empty an existing array

Scroll Down

A couple methods on how to empty an array in Javascript

Consider var my_array = [1, 2, 3]; and var other_array = my_array;

  • New empty array references remain unchanged

    Be aware that if you had any references to my_array they’ll remain unchanged as you would be pointing my_array to a newly created one.

    my_array = [壯陽藥
    punctuation">]; // other_array is still [1, 2, 3]

  • Setting array.length to 0

    Be aware that as we’re updating the value, all references to my_array will still point to the same changed array.

    my_array.length = 0; // other_array is now empty []

  • Array splice() method

    Be aware that as we’re updating the value, all references to my_array will still point to the same changed array.

    my_array.splice(0, my_array.length);
    // other_array is now empty []

  • Lodash’s remove() method

    Be aware that as we’re updating the value, all references to my_array will still point to the same changed array.

    _.remove(my_array, undefined);
    // other_array is now empty []