Introduction to Arrays

 1) Lets' now talk about our first data structure. And that's gonna be Arrays.

2) An Array is like a big container into which we can throw variables and then later reference them.

3) The two most important data structures, at-least in javascript are arrays and objects.

4) Array Literal syntax:-





5) A different way of creating an Array is by using Array function and new keyword:-



6) Accessing individual Array elements:-



we can access individual array elements by referencing them by index. The first index is 0.





7) Array.length property:-
Array.length property will give us the actual length of the Array. It will not respect the indexing rule and will give us the actual number of elements present in the Array.
**ignore this       console.log(friends) part in this code.





Since the number of elements in the friends Array is 3, friends.length will also give us the output: 3.

8) Accessing the last element of an Array by Array[Array.length-1] method.
** ignore the console.log       part.




since Array follows the indexing system friends.length = 3 , and friends[friends.length-1] = friends[3-1]
= friends[2]
and in the 2nd index the element is Peter

Hence the output.


9) Mutating the Array elements:
Mutating means changing the value of specific Array elements.

In this example we will mutate the value of index 2

** ignore the console.log       part.




In the output the value of index[2] has been changed from 'Peter'  to 'Jay'.

10) An Array can hold different data inside it. It can hold strings, numbers , an arithmetic operation, and even an another Array.








So the Jonas Array is holding:-

1) firstName- a variable
2)Schmedtmann- a string
3)2037-1991- arithmetic operation
4) teacher- a string
5) friends - an Array








11) Exercise:

Wrong way to access Array as an argument of the function









We have to access each element in the Array separately by using index system.











If we want to get the ages inside an Array, we can call the function inside an Array



Comments