UNDERSTANDING PURE JAVASCRIPT ARRAYS

By: Saurav

2017-11-29 01:07:00 UTC

ARRAYS

1. An array is a linear allocation of memory in which elements are accessed by integers that are used to compute offsets
2. They can be very fast data structures
3. Javascript does not have anything like this kind of array
4. Instead, Javascript provides an object that has some array like characteristics. It converts array subscripts into strings that are used to make properties. It is significantly slower than a real array, but it is more convenient to use.
5. Retrieval and updates of properties work the same as with objects except that there is a special trick with integer property names.
6. Arrays have their own literal format.
7. Array also have a much more set of built in methods than a normal object

ARRAY LITERAL

1. Provides convenient notation for creating new array values
2. An array literal is a pair of square brackets surrounding zero or more values separated by commas
3. An array literal can appear anywhere an expression can appear.
4. The first value will get the property name '0', the second value can get the property name "1" and so on.

var numbers = [ 'zero', 'one', 'two'...]

var number_object = {'0': 'zero', '1': 'one', '2': 'two'....}

both produces the same result. Both numbers and number_object are objects containing 3 properties, and those properties have exactly the same name and values. 

Note: But, there are also significant differences. numbers inherits from Array.prototype whereas number_object inherits from Object.prototype, so numbers inherits a larger set of useful methods (also. number has the length properties)

5. JavaScript allows mixtures of types of values to be the elements of an array as shown below:

var misc = ['string', 98.2, true,null,[1.2,1], Nan] 

ARRAY LENGTH


1. Every array has a length property.
2. Length property is the largest integer property name in the array plus one.
3. Length can be set explicitly. Making the length larger does not allocate more space for the array. Making length smaller will cause all properties with a subscript that is greater than or equal to the new length will be deleted.

4. A new element can be appended to the end of an array by assigning to the array's current length.

numbers[numbers.length] = 'shi'

It is more convenient to use the push methods instead to accomplish the same thing:

numbers.push('shi')

DELETE

1. Since JavaScript's arrays are really objects, the delete operator can be used to remove elements from an array: delete numbers[2] .
2. Unfortunately, it leaves a hole in the array. This is because the elements on the right retain their original names.
3. To handle this, JavaScript array have a splice method.
4. The first argument is the place of the element, the second one is the number of elements to delete. Any additional arguments get inserted in to the array at that point.

numbers.splice(2,1)

ENUMERATION

1. Since, java script array are really objects, the "for in" statement can be used to iterate over all of the properties of an array.
2."For in" does not guarantee about the order of the properties.
3. The "For" statement avoids there problems.

for (i =0; i< myArray.length; i+=1){
	document.writeln(myArray[i])
}

CONFUSION

1. JavaScript does not have a good mechanism for distinguishing between arrays and objects.
2. "typeof" returns object for an array as well

METHODS

1. The methods provided by JavaScript for array is stored in Array.prototype
2. Array.prototype can be augmented and we can add new methods for own purposes.
3. Because an array is an object, we can add methods directly on an individual array.
4. Although arrays are objects, it is not useful to use the Object.create method on arrays because it produces an object, not an array.
Note: We can always add a new method to compensate the lack of built in methods by using Array.new_method = function(){}

DIMENSION

1. JavaScript doesn't have arrays of more than one dimension but it can have arrays of arrays just like ruby

Owned & Maintained by Saurav Prakash

If you like what you see, you can help me cover server costs or buy me a cup of coffee though donation :)