In an Array , elements are zero-indexed i.e first element of an array is at index 0 and the last element is at the index equal to the value of array length minus 1 (array.lenght-1)
Example ->
var name = [ ' first ' , ' second '] ;
console.log( name[0] ) //output -> first
console.log( name[1] ) //output -> second
console.log( name[name.lengh - 1 ] ) //output -> second
How to access an Array elements ->
Array elements are object property in the same way as toString property but if you try to access an array directly then it will show an syntax error because the property name is not valid .
Example ->
console.log(name.0); // syntax error
So you can't access the array elements with dot notation , you must be accessed using bracket notation i.e [ ] .
Example ->
var names = [ " Mukesh " , " Ayush "] ;
console.log( names.0) ; // syntax error<br>
console.log( names.[0] ) ; correct syntax
0 Comment(s)