1

I'm fairly new to JavaScript beyond jQuery, and I was reading up on randomization in a JavaScript array & the shortcomings of using the Array.sort method with a random number. I see the recommendation instead is to use the Fisher-Yates shuffle. In looking at the JavaScript code for this method:

Array.prototype.randomize = function()
{
    var i = this.length, j, temp;
    while ( --i )
    {
        j = Math.floor( Math.random() * (i - 1) );
        temp = this[i];
        this[i] = this[j];
        this[j] = temp;
    }
}

I'm struck by this line:

var i = this.length, j, temp;

What's going on here? Is a variable being given multiple values, or is this shorthand for something?

1

6 Answers 6

5

A variable can never have multiple values at the same time.

The code you give is shorthand for

var i = this.length;
var j;
var temp;

Syntax like that above is legal in most programming languages.

Sign up to request clarification or add additional context in comments.

Comments

2

var i = this.length, j, temp;

is the same as :

var i = this.length;
var j; // here the value is undefined
var temp; // same, temp has a an undefined value

Comments

2

No, it's the shorthand for: var i = this.length; var j; var temp;

Comments

1

You are creating three variables, and only the leftmost will be born with a value - in this case, whatever the value this.length is.

As pointed out by everyone else respondig to you question, it's the same as:

var i = this.length;
var j, temp;

Other languages like Java, C# and Visual Basic allow you to create variables with a similar syntax. I.E.:

I.e.:

// C# and Java
int i = this.length, j, temp;

// which is the same as:
int i = this.length;
int j, temp;

 

' Visual Basic
Dim i = this.length as Integer, j as Integer, temp as Integer

' Which is the same as:
Dim i = this.length as Integer
Dim j as Integer, temp as Integer

Comments

1

It's just multiple declaration of variables in a single line. It's equivalent to this:

var i, j, temp;
i = this.length;

Which is equivalent to this:

var i;
var j;
var temp;
i = this.length;

Comments

1

The specification defines the variables statement to be the var keyword, followed by a list of variables declarations, separated by a comma:

VariableStatement :
    var VariableDeclarationList ;

VariableDeclarationList :
    VariableDeclaration
    VariableDeclarationList , VariableDeclaration

Note the recursive definition of VariableDeclarationList. It means that an unlimited number of variables declarations can follow a var keyword.

Hence

var foo, bar;

is the same as

var foo;
var bar;

Related question: What is the advantage of initializing multiple javascript variables with the same var keyword?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.