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

  • Home
  • SEARCH
  • 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 6866185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:09:01+00:00 2026-05-27T03:09:01+00:00

Is there a function in JavaScript similar to Python’s range() ? I think there

  • 0

Is there a function in JavaScript similar to Python’s range()?

I think there should be a better way than to write the following lines every time:

array = new Array();
for (i = 0; i < specified_len; i++) {
    array[i] = i;
}
  • 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-27T03:09:02+00:00Added an answer on May 27, 2026 at 3:09 am

    No, there is none, but you can make one.

    JavaScript’s implementation of Python’s range()

    Trying to emulate how it works in Python, I would create function similar to this:

    function range(start, stop, step) {
        if (typeof stop == 'undefined') {
            // one param defined
            stop = start;
            start = 0;
        }
    
        if (typeof step == 'undefined') {
            step = 1;
        }
    
        if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
            return [];
        }
    
        var result = [];
        for (var i = start; step > 0 ? i < stop : i > stop; i += step) {
            result.push(i);
        }
    
        return result;
    };
    

    See this jsfiddle for a proof.

    Comparison between range() in JavaScript and Python

    It works in the following way:

    • range(4) returns [0, 1, 2, 3],
    • range(3,6) returns [3, 4, 5],
    • range(0,10,2) returns [0, 2, 4, 6, 8],
    • range(10,0,-1) returns [10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
    • range(8,2,-2) returns [8, 6, 4],
    • range(8,2) returns [],
    • range(8,2,2) returns [],
    • range(1,5,-1) returns [],
    • range(1,5,-2) returns [],

    and its Python counterpart works exactly the same way (at least in the mentioned cases):

    >>> range(4)
    [0, 1, 2, 3]
    >>> range(3,6)
    [3, 4, 5]
    >>> range(0,10,2)
    [0, 2, 4, 6, 8]
    >>> range(10,0,-1)
    [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
    >>> range(8,2,-2)
    [8, 6, 4]
    >>> range(8,2)
    []
    >>> range(8,2,2)
    []
    >>> range(1,5,-1)
    []
    >>> range(1,5,-2)
    []
    

    So if you need a function to work similarly to Python’s range(), you can use above mentioned solution.

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

Sidebar

Related Questions

Is there a way to listen for a javascript function to exit? A trigger
Is there any way that I can call a JavaScript function via css? For
Is there a way to kill the unload function with javascript(jquery)? I am looking
I have a javascript function that checks for a date range. Is there any
Is there any way I can make Firefox call a JavaScript function in my
Is there any way I can expose a C++ object/function to JavaScript running inside
If a javascript function is declared anonymously is there any way to override it
Is there any globals function in Javascript which is similar to PHP globals that
Is there an equivalent function in JavaScript or jQuery similar to strpos in PHP?
In javascript there is function getImageData(), is there any function in PHP similar to

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.