Skip to content Skip to sidebar Skip to footer

Javascript: Can A Variable Have Multiple Values?

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.

Solution 1:

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.

Solution 2:

var i = this.length, j, temp;

is the same as :

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

Solution 3:

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

Solution 4:

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 Javaint i = this.length, j, temp;

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

 

' Visual BasicDim i = this.length asInteger, j asInteger, temp asInteger' Which is the same as:Dim i = this.length asIntegerDim j asInteger, temp asInteger

Solution 5:

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;

Post a Comment for "Javascript: Can A Variable Have Multiple Values?"