Skip to content

Instantly share code, notes, and snippets.

@nzakas
Created January 1, 2011 22:51
Show Gist options
  • Select an option

  • Save nzakas/762069 to your computer and use it in GitHub Desktop.

Select an option

Save nzakas/762069 to your computer and use it in GitHub Desktop.
/*
* Pre-ECMAScript 1, JavaScript didn't have native arrays.
* Those who needed them frequently used functions such as the following.
* Also note that there was no special "undefined" value.
*/
function makeArray(len){
var array = new Object();
array.length = len;
//initialize each item with null (empty placeholder)
for (var i=0; i < len; i++){
array[i] = null;
}
return array;
}
var colors = makeArray(4);
colors[0] = "red";
colors[1] = "blue";
colors[2] = "green";
colors[3] = "black";
@timdown
Copy link

timdown commented Mar 14, 2013

A couple of years of learning to be slower to judge and generally nicer on the internet makes me a bit shocked at my efforts here. Sorry again, Nicholas.

@garethmaybery
Copy link

Nicholas, thank you for this! Just started coding in JavaScript and this piece of code really helped me solve my problem I was having with creating multiple arrays with a function.

@artidataio
Copy link

It is 2023, you can write colors this way now:

let colors = ["red", "blue", "green", "black"]

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