Skip to content

Instantly share code, notes, and snippets.

@demonixis
Created April 15, 2015 13:51
Show Gist options
  • Select an option

  • Save demonixis/6a92a01520746efdc934 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

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

@samuelstein

Copy link
Copy Markdown

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

@vipinmavi-dev

Copy link
Copy Markdown

thanks bro.

@dzonekl

dzonekl commented Apr 4, 2019

Copy link
Copy Markdown

So which elements are kept?

@makstaks

Copy link
Copy Markdown

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
Copy Markdown

Thanks bro

@AbisoyeAlli

Copy link
Copy Markdown

this is super helpful

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