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 8151389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:22:20+00:00 2026-06-06T15:22:20+00:00

I’ve put together a little range function in JS. I’ve tested it in Chrome

  • 0

I’ve put together a little range function in JS. I’ve tested it in Chrome 19, FF, and IE (7-9) and it’s working well. The question I have has to do with the while statement.

function range(from,to,step)
{
    'use strict';
    var sCode,eCode,result;
    result = [];
    step = (!step || isNaN(step) || step === 0 ? 1 : step);
    sCode = (''+from).charCodeAt(0);
    eCode = (''+to).charCodeAt(0);
    step *= (sCode > eCode && step > 0 ? -1 : 1);
    do
    {
        if (String.fromCharCode(sCode))
        {
            result.push(String.fromCharCode(sCode));
        }
    }while((step > 0 && eCode >= (sCode+=step)) || (step < 0 && eCode <= (sCode+=step)));
    return result;
}

I remember reading a question here a while back on how JS handles Control flow constructs and logical operators. I think it had something to do with checking if an object had a certain method, and if so, using it’s return value (if (event.returnValue && e.returnValue === true) kind of stuff).
I can’t seem to find that question any more, here’s what I wanted to know:

while((step > 0 && eCode >= (sCode+=step)) || (step < 0 && eCode <= (sCode+=step)));

Since the function behaves as I want it to, I think I’m right in saying that, if step < 0 is false, && eCode >= (sCode+=step) will be ignored, leaving the value of sCode unchanged.
When the step check is true, sCode will be in/decremented. I’ve put this assignment in brackets, to make sure that the newly assigned value of sCode will be compared to eCode. Again, I assume that the brackets will give priority to the assignment over the logical operator.

Is this true for ALL browsers, or is it browser specific to some extent? is there a chance that this function will increment (or decrement) the value of sCode twice in some browsers?
In this case, it’s not that important (it’s an easy fix to prevent any issues). But I want to know if this behaviour is inherent to JavaScript itself, or to the browser implementation.


Thanks for reading this far down. If you don’t mind
A couple of other things (not important, but just wondering):

  • what is the max charCode in JavaScript? A quick look on google didn’t tell me, and testing in the JS console lead me to believe this was 5999999999989759 which seems almost incredible, but then again I might need to brush up on my Chinese.
  • When from is undefined, the (jslint approved) approach from.toString().charCodeAt(0); fails, because obviously, undefined had no toString method, why the, does (''+from).charCodeAt(0); return U all the same? I thought it implicitly called the toString method?
  • 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-06-06T15:22:23+00:00Added an answer on June 6, 2026 at 3:22 pm

    I think I’m right in saying that, if step < 0 is false, && eCode >=
    (sCode+=step)
    will be ignored, leaving the value of sCode unchanged

    Correct. If the first operand evaluates to false, the second operand will not be evaluated.

    After when the step check is true, sCode will be in/decremented. I’ve
    but this assignment in brackets, to be sure that the newly assigned
    value of sCode will be compared to eCode. Again, I assume that the
    brackets will give priority to the assignment over the logical
    operator.

    Correct again, but the parentheses around the assignment are required, since assignment has a lower precedence than a comparison.

    Is this true for ALL browsers?

    Yes. I would be incredibly suprised if you find one that doesn’t behave this way.

    When from is undefined, the (jslint approved) approach
    from.toString().charCodeAt(0); fails, because obviously, undefined had
    no toString method, why the, does (''+from).charCodeAt(0); return U
    all the same?

    Because it concatenates the value of from with the empty string. The value of from is undefined, which is coerced to a string, and you end up with the string “undefined”, and the character at index 0 of that string is “u”.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.