-1

I don't understand var a = [], i here. How can a be declared as both an array and whatever i's type is?

    // Source: Javascript: The Good Parts, p. 63
    Array.dim = function (dimension, initial) {
         var a = [], i;
         for(i = 0; i< dimension; i +=1)
         {
            a[i] = initial;   
         }
        return a;
    }
1

5 Answers 5

2

it means declare both (separately) - not declare them to be equal

same thing as:

var a = [];
var i;
Sign up to request clarification or add additional context in comments.

5 Comments

Yes 'i' it's also a var. As Trebuchet mentioned, it's like having 2 lines of 'var xx' declarations.
Yes, i is a var. As this answer states clearly: the above code is the same thing as declaring both variables on separate lines. The comma separates variable declarations.
@Kevin it's all about personal preference. I tend to prefer the separate version.
I personally hate the idea of declaring multiple variables on the same line, but some people like it. In the example you've provided, I think it's a horrible idea to declare both variables on the same line. If, for some reason, i was not declared by that line, it would become a global variable. In my opinion, the notation in your example makes it much easier to forget to declare i (or easier to delete that line and forget that i was declared there). Why not just include the var keyword in the for loop??
I wouldn't normally declare multiple variables on the same line, but I do declare them in the same var statement with line-breaks after each comma...
2

The following code:

var a = [], i;

is EXACTLY the same as this code:

var a = [];
var i;

It means:

  1. a is an empty array
  2. i is an uninitialized var

Comments

1

Javascript variables don't have types.

a is initialized to an array; i is not initialized at all.

Nothing prevents you from later writing

a = 42;
i = ["Hi", "there!"];

Comments

0

Javavscript variables does not have any types. Here a is initialized as an array

var a = []; //Same as var a= new Array(); 

and i can have any value

var i = 0;// Here it is an integer
i = "Hello";// String. Type changed to string. But it is not a good practice

Read JavaScript Variables and DataTypes

Comments

0

var a = [], i literally means define a as an array and define i as a variable. Nothing is actually assigned to i.

For example:

var a = 5,
    b = 8,
    c = "string";

This basically allows you to define 3 variables without having to use the word var 3 times. Shorthand JavaScript.

Defining your variables before you use them prevent errors if the variable is not present, making functions more reliable. For example, not defining x as a variable will result in:

if(x) { /* native code */ }

Throwing an error but:

if(window.x) { /* native code */ }

Will not, because it checks the window object for x, where all global variables are stored.

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.