Skip to content

Instantly share code, notes, and snippets.

@demonixis
Created April 15, 2015 13:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demonixis/6a92a01520746efdc934 to your computer and use it in GitHub Desktop.
Save demonixis/6a92a01520746efdc934 to your computer and use it in GitHub Desktop.
Resize an array with JavaScript is easy
var array = [1, 2, 3, 4, 5];
console.log(array.length); // 5
array.length--;
console.log(array.length); // 4
array.length += 15;
console.log(array.length); // 19
@sauravsahu02
Copy link

Short and useful example. So basically length serves dual purpose.

@samuelstein
Copy link

this changes only the length and does not add null values.

@vipin95
Copy link

vipin95 commented Aug 6, 2018

thanks bro.

@dzonekl
Copy link

dzonekl commented Apr 4, 2019

So which elements are kept?

@makstaks
Copy link

So which elements are kept?

Here's a running example @dzonekl, the elements at the end of the array are removed.
https://runkit.com/makstaks/resizing-an-array-in-javascript

@phanthaiduong22
Copy link

Thanks bro

@AbisoyeAlli
Copy link

this is super helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment