Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6008171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:47:13+00:00 2026-05-23T01:47:13+00:00

What would be the tersest way to create this array: var x = [1,

  • 0

What would be the tersest way to create this array:

var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
         11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

For example, a for loop:

var x = [];
for (var i=1;i<=20;i++) {
  x.push(i);
}

Or a while loop:

var x = [], i = 1, endInt = 20;
while (i <= endInt) {
  x.push(i);
  i++;
}

Would there be other examples that would be terser — in other words — less code? I’m thinking of things like in Ruby where the equivalent code I believe would be as simple as 1..20. I’m not aware of syntax like that in JavaScript but I’m wondering if there are shorter ways to do that same thing.

UPDATE: I wasn’t thinking of removing semicolons or var for answers in the question, but I have to admit the question implies that. I am more curious about algorithms than shaving bytes. Sorry if I was unclear! Also, making it into a function is simple enough, just slap function range(start, end) { /* guts here */ } around it and you’re there. The question is are there novel approaches to the “guts.”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:47:13+00:00Added an answer on May 23, 2026 at 1:47 am

    Favorite method

    Update Sep13,2015:

    Just came up with this new method which works with browsers which support the ES6 standard:

    > Array(5).fill().map((x,i)=>i)
    [0, 1, 2, 3, 4]
    

    Note the above does a tiny bit of extra work (fills with undefined) but is relatively minor vis-a-vis the speedup you can achieve by using a for loop, and if you forget the .fill you may be confused why your array is mysteriously [empty x 5]. You can encapsulate the above as a custom function, or alternatively use a somewhat more intended method:

    > Array.from(Array(5),(x,i)=>i)
    [0, 1, 2, 3, 4]
    

    You can of course directly go from that into whatever you want to do, like python’s list comprehensions e.g. [i**2 for i in range(5)]:

    > Array.from(Array(5), (_,i)=> i**2)
    [0, 1, 4, 9, 16]
    

    … or if you want to get more complicated…:

    > Array.from(Array(5), (_,i)=> {
        const R = /*some computation*/;
        return /*etc*/;
    });
    

    [edit May,2021]: theoretically tersest way of defining such a function nowadays is f=i=>i?[...f(i-1),i]:[], where you replace f with range1 or whatever the name is, but which would be very slow (quadratic complexity) due to intermediate structures so should never be used. f=i=>i?f(i-1)&&x.push(i)&&x:x=[] is linear complexity but relies on abuse of notation and is unreadable and pollutes global variables as well. But, since defining arrow functions (which don’t bind but rather inherit this) is pretty terse nowadays, you could just wrap the above solution:

    const range1 = n=> Array.from(Array(n), (_,i)=> i+i);
    // range1(5)==[1, 2, 3, 4, 5]
    

    Circumstantially, the tersest way to do a range(N), if you already have a list lying around of exactly that length N, is just to map it: e.g. rather than do Array.from(Array(myArr.length), (_,i)=> i**2), you would just do myArr.map((_,i)=> i**2). (This has no side-effect unless you want it to.)


    everything below is historical:

    After thinking about it a bit, this is the shortest implementation of the standard range(N) function in JavaScript I could come up with:

    function range1(i){return i?range1(i-1).concat(i):[]}
    

    Note: Do not use this in production; it’s O(N^2)

    Contrast with current top-voted answer:

    function range1(i){var x=[];var i=1;while(x.push(i++)<i){};return x}
    

    Example:

    > range1(5)
    [1, 2, 3, 4, 5]
    

    This is like the poster child for recursion, though I was expecting it to be longer until I thought of ternary-if-statement, which brings it down to 42 necessary characters.

    Note that the "standard" range function returning [start,end) can be written by doing .concat(i-1).


    Update: Ooh, I discovered an incredibly short version with ugly imperative syntax by abusing for loops, reverse-ordering, the fact that assignments return a value: for(y=[],i=20;y[--i]=i;){} consisting of only 25 characters (though you will want var y which you can insert into a for loop, and +1 if you don’t want 0…19). While it is not shorter if you need to define a function, it is shorter than i?r(i-1).concat(i):[] if you do not need to make a function.


    Added some performance profiling testcases: it seems that everything besides a standard in-order for-loop is 10x slower, at least on V8. https://jsperf.com/array-range-in-javascript
    (Of course, none of this matters if you’re programming in a functional style anyway and would hit every element with a function call anyway.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would you recommend the best and easy way to transfer (or copy) the subscriptions
Would anyone know how to loop through all ID's that being with name_ So,
Would it not make sense to support a set of languages (Java, Python, Ruby,
Would it be possible to print Hello twice using single condition ? if condition
Would it be possible to show an image in full screen mode using silverlight.
Would the following SQL remove also the index - or does it have to
Would having a nice little feature that makes it quicker to write code like
Would it be wwwroot, C, the root virtual directory where the assets are hosted,
Would a C++ CLI compiler be able to compile some large sets of C++
Would NTFS allocation blocks of 16KB or 32KB make compile time faster in comparison

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.