what's difference to declare array:
var tab={};
AND
var tab=[];
There is a big difference:
tab = {} // an empty object (or a blank object)
tab = [] // an array (which is actually an object too)
Object-Oriented JavaScript - Second Edition: What is an array? It's simply a list (a sequence) of values. Instead of using one variable to store one value, you can use one array variable to store any number of values as elements of the array. To declare a variable that contains an empty array, you use square brackets with nothing between them:
var a = [];
To define an array that has three elements, you do this:
var a = [1, 2, 3];
The elements contained in an array are indexed with consecutive numbers starting from zero. The first element has index (or position)
0
, the second has index1
, and so on.To access an array element, you specify the index of that element inside square brackets. So,a[0]
gives you the first element of the arraya
,a[1]
gives you the second, and so on.
Difference:
An object is similar to an array, but with the difference that you define the keys yourself. You're not limited to using only numeric indexes and you can use friendlier keys, such as first_name, age, and so on.
var tab = {
name: 'Ninja'
};