JS Array

//Foreword, this article was written in haste, please forgive for formatting of article,
//but I am sure you will take away something.

//Array
/**
* An Array is a collection of items, usually it is a collection of similar type of objects
* which is true for a typed safe language like Java,C# etc, but js is not a strictly typed language
* JS array can hold heterogenous items.
*/

//JavaScript also has a second value that indicates absence of value.
//for example var myVar; and if we print this variable then
//it will print undefined, because variable has no value assigned.


var arr = [3, { a: "itm1", b: "itm2" }, 7, function () { console.log("under func"); }];
//above array arr is an heterogenous item array, it compose of numbers 3,7 and object {a:"itm1",b:"itm2"}
// also it holds a reference to function object.
console.log(arr[0]);//will print 3.
console.log(arr["2"]);//will print 7, an index can be string number(internally treated as number), note only string number.
console.log(arr[1]);//will print the object {a:"itm1",b:"itm2"} .
console.log(arr[123]);//will print undefined, note in JS any array index not defined
//will not throw ArrayIndexOutOfBoundsException like Java.
//accessing anything not defined will print undefined.
console.log(arr[3]);//will print [Function], a pointer to function
console.log(arr[3]());//will call the function and print the "under func"

//in array one can add a new item at any index(if index already exists will replce the value else get defined)
arr[10] = "ravi";//adding item at index 10
arr["11"] = "sonu";//adding item at index 11 (string number, internally treated as number)
console.log(arr);//will print [ 3, { a: 'itm1', b: 'itm2' }, 7, [Function], , , , , , , 'ravi', 'sonu' ]
//as you see, items at index 10,11 got created while other index posiiton like 4,5,6,7,8,9 are just undefined
//assessing index 7 will print undefined
console.log(arr[7]);//undefined

//Rule of Thumb, accessing any thing not defined is not a problem in javascript, it will say undefined only.
//however accessing anything on undefined will throw error like
//console.log(arr[7].demo);//will print TypeError: Cannot read property 'demo' of undefined
//here already arr[7] is undefined and user is asking to access a property demo which itself not exists
//however if arr[7] would have existed and demo property of that element if not exist then
//system would have printed undefined.
//like
console.log(arr[1].b);//will print "itm2"
console.log(arr[1].c);//will print undefined

//till now we have learnt that array item can be inserted at any index No.(either a numeric indexNo or string Number)
//although you can add a key value item(not recommended at all) to an array as item, for instance:
arr["mykey"] = { ob: "oop", er: 12 };
console.log(arr);//will print below:
/**[ 3,
{ a: 'itm1', b: 'itm2' },
7,
[Function],
,
,
,
,
,
,
'ravi',
'sonu',
mykey: { ob: 'oop', er: 12 } ]
*/

//, note if you add key value item it will always get allocated to the last in the array (not at any index)
console.log(arr[12]);//you might be thinking that Js has allocated the element at 12 index, but its not the case
//for such addition of items js keep them as an key value pair only, which can only be accessed using the key only
//not by index.
console.log(arr["mykey"]);//will print { ob: 'oop', er: 12 }
console.log(arr.mykey);//will print { ob: 'oop', er: 12 }
console.log(arr.mykey.er);//will print 12

//I have tried best to all possible ways an array can be defiend even the non recommeneded way
//just to let you know that although Js will not oppose you, but good programming paradigm will.
//always make pratice to use accepatble form of array, better make homogeneous item array.

Comments

Popular posts from this blog

Custom Module Export

JS prototype