Sign up ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Obviously this challenge would be trivial with separate functions and libraries, so they aren't allowed.

Your code must conform to an ECMAscript specification (any spec will do), so no browser-specific answers.

The array must be accessible after it is instantiated.

I have an answer that I'll withhold for now.

Note: this challenge is specific to javascript because it is notoriously inconvenient to make multi-dimensional arrays in javascript.

share|improve this question
I'm at 57 characters. Is this a good score? – Jan Dvorak Jan 24 '13 at 23:12
Ok, definitely not :-) – Jan Dvorak Jan 25 '13 at 10:26
Are you sure you don't just want this nice 1x1 array of 10s? [10] – kojiro Jan 28 '13 at 4:42
Would that be [[10]]? – Anonymous Mar 12 '13 at 21:50

20 Answers 20

up vote 6 down vote accepted

Javascript ES6, 30


ES6 in action :)

share|improve this answer
Hi, this question is like 2 years old. It's probably a better use of your JS code golfing skills to answer more recent questions ;) – George Jun 18 at 22:47
@George I don't think there's anything wrong with answering old challenges. (Some challenge authors will also update the accepted answer if a new winner comes along, no matter how old the challenge is.) – Martin Büttner Jun 19 at 7:18
@MartinBüttner This answer is not elligible for being accepted as ES6 is younger(it came in 2015 :)) than this challenge, and Array.fill() is ES6 only :) – Katenkyo Jun 19 at 8:24
I just answered because I saw other ES6 answers too. I don't think there is anything wrong with this. The author might update his question and others might take advantage of multiple ways of solving the problem above. – catgocat Jun 19 at 10:33
ES6 has been accepted long before its draft actually got frozen (that is in 2015, a few days back). I am not sure though whether that is correct or incorrect. – Optimizer Jun 19 at 11:10

Javascript, 34 bytes


Since it's apparently OK to make the rows equal by reference, I guess it's apparently OK to rely on that fact. This helps us shave off one for-loop by building the table at the same time as its rows. So, here's my new contestant.

Since r[0] and b[0] are overwritten during the loop, they can contain garbage. This gives us another free execution slot to shave off some commas. Sadly, r=b=[] won't do, since then they are equal by-ref.

Also, it scales well (99x99 is still 34 bytes), doesn't require ES5 (the new functions have terribly long names, anyways), and as a bonus, it's unreadable :-)

share|improve this answer
Beautiful. Absolutely beautiful. – Briguy37 Jan 25 '13 at 16:27
Damn, I ended up with: c=[b=[i=10]];while(i--)b[i]=c,c[i]=1. Should have known!!! – mowwwalker Jan 27 '13 at 22:35

ECMAScript 6 - 33 Characters


Outputs a 10x10 array of "1"s.

This abuses the fact that the string "1111111111" has all the requisite properties to be treated as if it is an array so you can use the spread operator ... to transform it into an array of characters and then map it to a copy of the array with each element referencing the "original".

Or with only one variable name (for 35 characters):


Or for extra confusion (but at 45 characters)


or (43 characters)

share|improve this answer
Nice abuse of the ... operator! – nderscore May 23 '14 at 19:34

44 bytes


Previous version:

share|improve this answer
Very clever update! – Ṣhmiddty Jan 25 '13 at 4:40
for(a=[i=9];i;)a[i--]=[1,1,1,1,1,1,1,1,1,1] – AmericanUmlaut Jan 25 '13 at 10:25
+1, two very nice solutions. – grc Jan 25 '13 at 14:05
@AmericanUmlaut that doesn't work – copy Jan 25 '13 at 16:04
I like ^ for != – Jan Dvorak Jan 25 '13 at 17:21

45 characters


Each element points to the same array, but the spec doesn't mention anything against that!

share|improve this answer
the spec says that "The array must be accessible after it is instantiated.". I guess that means you should have an extra x= at the beginning – w0lf Jan 25 '13 at 13:21
a very nice solution nevertheless (+1) – w0lf Jan 25 '13 at 15:46

Javascript, 51


Or if all indices are allowed to point to the same array:

Javascript, 41

share|improve this answer
All of the indices now point to the same column, which is probably unwanted. (Try a[0][1] = 5; console.log(a[1][1])). – copy Jan 25 '13 at 1:13
Yeah I didn't see that. I've updated my answer. – grc Jan 25 '13 at 2:38

JavaScript, 45 44 Bytes

Best I have so far. (Still trying).


Shorter (thanks mellamokb!)

share|improve this answer
Note that for is almost always preferable to while when golfing. The basic statement is the same length (while() vs for(;;)), but for gives you more flexibility. In this case, you could put x=[] in the initializer of the for and save one character. – mellamokb Jan 25 '13 at 14:49
@mellamokb, thanks for the tip :-) – FakeRainBrigand Jan 25 '13 at 15:16

Javascript, 47

Since my original solution has been beaten, I will now post it.


Unfortunately, 0x3FF.toString(2) isn't quite as efficient as just listing the string out, which isn't quite as efficient as just statically declaring the array.

You can shave off one character this way (46):


You can save 2 more characters like so: (44)


Another 44 byte solution using JS 1.8 function expression closures (FF only atm):

share|improve this answer
You can save one char by initializing i in a like this: a=[i=10] – air_blob Jan 31 '13 at 9:44

JavaScript, 57 bytes


Before golfing:


Note: This needs ES5, so don't expect much from IE.

share|improve this answer
Wow! I wrote eval('a=['+(b=Array(11)).join('['+b.join("1,")+'],')+']'). Apart from having different quotes and my having the variable inside eval, these are exactly the same – mowwwalker Jan 27 '13 at 22:39
a=eval("["+Array(11).join("[1,1,1,1,1,1,1,1,1,1],")+"]") has 56 – Anonymous Mar 12 '13 at 21:58


This has already been beaten, but here's my solution:

function x(){return[1,1,1,1,1,1,1,1,1,1]}x=x().map(x)
share|improve this answer
Arrow functions will do interesting things to JavaScript golfing. Now if only I could figure out how to trivially Curry builtins. – kojiro Jan 25 '13 at 4:56
You'd need to insert x= before x().map (or y=, etc) to make it accessible, right? – Ṣhmiddty Jan 25 '13 at 4:56
@Schmiddty Yes, so updated. – kojiro Jan 25 '13 at 5:02

39 bytes

Using array comprehension in ECMAScript 7:

x=[x for(y in x=[1,1,1,1,1,1,1,1,1,1])]
share|improve this answer
There are no array comprehensions in ES6, that's an ES7 proposal. – catgocat Jun 19 at 14:19
@afonsomatos at the time I made this answer, it was still part of the ES6 draft. It's updated now though :) – nderscore Jun 19 at 14:29
Replacing [1,1,1,1,1,1,1,1,1,1] with Array(10).fill(1) shaves 4 chars. – Ṣhmiddty Jun 19 at 21:36

56 characters

Rather long answer, but will be better for 100x100 etc.

share|improve this answer

ES6, 55

It's not the shortest, but I thought I'd go for a different approach to what's already here.


This is a whole load of confusing.
- Cast this (window) to a string
- Split the string into chars
- Grab a slice of just 10
- Map the result of mapping 1 onto the initial slice

share|improve this answer
Where can I test it? It doesn't work right in my FF. – Qwertiy Feb 5 at 12:48
I'd missed out some parens, however, because a already existed that version seemed to have worked. Updated version should work in FF and node with the --harmony flags. – Dan Prince Feb 5 at 14:31
Now it returns right array, but it's not assigned to any variable. – Qwertiy Feb 5 at 15:27

61 for fun

share|improve this answer

Javascript, 36 chars (ECMA 6)


Tested in my browser (Firefox on Ubuntu) and in the Codecademy Javascript interpreter

share|improve this answer

Javascript - 64 63 59 characters

for(var a=b=[],i=100;i-->0;b[i%10]=1)if(i%10==0){a[i/10]=b;b=[]}

for(var a=b=[i=100];i-->0;b[i%10]=1)if(i%10==0){a[i/10]=b;b=[]}

One char saved by hiding the iterator in the array.


Saved some chars with Jans suggestions.

Readable version:

for(var a = b = [i = 100]; i-- > 0; b[i % 10] = 1) {
  if(i % 10 == 0) {
    a[i / 10] = b;
    b = [];

Yes, it's not the shortest solution, but another approach without using readymade [1,1,1,1,1,1,1,1,1,1] arrays.

share|improve this answer
Change i-->0 to i--. While the "downto" operator is nice, it's not really neccessary. ==0 could be shaved off by two bits as well. Also, leave out var. While normally useful, four useless bytes are just unacceptable here. – Jan Dvorak Jan 25 '13 at 18:06

Javascript 69 characters (for now)

share|improve this answer

JavaScript 73

function h(){s="1111111111",r=[10],i=-1;while(++i<10){r[i]=s.split("");}}
share|improve this answer
r=[10], does nothing as you immediately overwrite r[0]. You don't need to declare s and could just do "1111111111".split("") but that is 22 characters whereas [1,1,1,1,1,1,1,1,1,1] is only 21 characters. – MT0 May 25 '14 at 7:18

Javascript ES6, 53


But the ones in the array are strings :)

share|improve this answer

Javscript ES6, 53

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.