Possible Duplicate:
how to empty an array in JavaScript
How to remove all items from jQuery array?
I have array var myArray = [];, I want to clear all items in this array on every post back.
Possible Duplicate:
how to empty an array in JavaScript
How to remove all items from jQuery array?
I have array var myArray = [];, I want to clear all items in this array on every post back.
Simplest thing to do is just
myArray = []; again.
edit — as pointed out in the comments, and in answers to other questions, another "simplest thing" is
myArray.length = 0; and that has the advantage of retaining the same array object.
$myArray.length solution works fine, while this does not in this use-case: var obj = {arr: []}; var a = obj.arr; a.push('elem'); a.length = 0; console.log(obj); vs var obj = {arr: []}; var a = obj.arr; a.push('elem'); a = []; console.log(obj);There is no such thing as a jQuery array, that's just a javascript array. When a page posts back, it re-renders and all of the javascript is re-run, you don't need to clear the contents of the array.
if, during execution of the page, you wanted to clear a javascript array, just re-initialize it as a new, blank array:
myArray = []; // no var, we are just initializing not declaring
jQueryarray.