Saturday, July 26, 2014

Arrays

-The difference between objects and arrays ?

  • objects are variables with multiply properties and methods 
  • Arrays are also special kind of objects because it has its own methods and properties 
  • arrays are variables that can store more than one value in one variable so there can be an array of objects and the arrays looks like this: 
        this is an imaginary pic of how the array stored in the computer memory and if you don't know much               about variables you can check this Variables in Javascript and Variables life time 

here its an array x of 12 elements 

-Facts about Arrays
  • each element in the arrays has its index 
  • by default the first element in the array is indexed by 0 so if we have array of 10 elements the index of the last element will be 9
  • your array elements are the same as the array data type so you cant put an integer into a string array expect the data type is var so you can put anything you want
  • facts is a very good choice for programmers to reduce the using of the limited resources for example if we have a school which have 1000 students we can make object of student which have the name,address,tel....etc and then make one or two arrays of object student its much better than building 1000 object for each student/
-Creating Arrays:

  •   whats the syntax to create an array lets :

                 variable DataType array_name=[value1,value2,value3.........];

               example:


              another example for string array:


  • Another way to create an array which is be the keyword "new" this will create an array in the memory:


-How to access any element in the Array:
we said that each element has its own index so simply we can use that index to get the value in that index 

y=beginners why?
because we said that the index starts with 0 so if we want to get the "Blog" we should get it using the x[2] because its 0,1,2 thats very important 

Note: this is not an advanced way to get values from indexes 


and here's another example:


-Arrays methods and and properties:
  • array_name.length: this will return the number of array elements
  • array_name.sort(): it'll sort the array elements according to alphabetical order




------as always this was the most important things about arrays remember guys its a beginners blog so i just have to make things simple and clear for you and then you should go on with it----------------------------




No comments:

Post a Comment