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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:00:53+00:00 2026-06-05T13:00:53+00:00

So I was reading about shuffling an array. And then I came across this

  • 0

So I was reading about shuffling an array. And then I came across this script:

shuffle = function(o){ //v1.0
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

When I look closely, the for does not even have any {} at all! But it is working, like magic. I am very curious to know how it work. (and the bunch of commas too.)

  • 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-05T13:00:55+00:00Added an answer on June 5, 2026 at 1:00 pm

    What follows the for () can be any statement; that can be something with curly braces, or it can be a single expression, or it can be an empty expression. for (...); is equivalent to for (...) {}. Naturally, this should only be used in conjunction with a for-loop which will terminate naturally, or you’ll have an infinite loop on your hands.

    The commas are effectively second-grade semicolons; they make for mostly-separate statements, but which will work in a for loop (and elsewhere; this is a very sloppy definition of them).

    for (
         // initialisation: declare three variables
         var j, x, i = o.length;
         // The loop check: when it gets to ``!i``, it will exit the loop
         i;
         // the increment clause, made of several "sub-statements"
         j = parseInt(Math.random() * i),
         x = o[--i],
         o[i] = o[j],
         o[j] = x
    )
        ; // The body of the loop is an empty statement
    

    This could be put in a more readable form as:

    for (
         // initialisation: declare three variables
         var j, x, i = o.length;
         // The loop check: when it gets to ``!i``, it will exit the loop
         i;
         // note the increment clause is empty
    ) {
         j = parseInt(Math.random() * i);
         x = o[--i];
         o[i] = o[j];
         o[j] = x;
    }
    

    As a while loop, that could be:

    var j, x, i = o.length;
    while (i) {
         j = parseInt(Math.random() * i);
         x = o[--i];
         o[i] = o[j];
         o[j] = x;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading about imports in python and came across this effbot article about
While reading about mcv3 I came across an attribute name called [ActionName] . It
I'm reading about the initialized values by default of an array/struct and have this
While reading about shell scripts and temporary file handling, I came across Symlink Exploits.
While reading about synchronization, I came across monitor pattern to encapsulate mutable states. The
While reading about Google webtool kit, came across a statement saying 'synchronous RPCs are
Reading about Kohana templates and saw something I've never seen before: $this->template->title = __('Welcome
Reading about Django, I saw this: http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#ref-contrib-admin - the fancy simple to use admin
While reading about exception, I will always come across checked exceptions and unchecked exceptions,
In reading about C#, I have come across the terms data transfer type and

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.