Search the Blog

Thursday, August 8, 2019

Javascript Arrays and Objects Concept with code and Logic and sample example

Defining an array in JavaScript-
                  var    javascript_array_variable = [];


   An array is a set of variables and objects. For example:
         var   fav_Fruits = ["mango", "orange", "banana"];
         var  fav_cars = ["Toyota", "Ferrari", "Lexus"];
         var best_friend = ["Sandeep", "Shiv", "Amit"];
         var fav_no = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31];
         var randomVariables = [2, "any type works", undefined, null, true, 2.51];



To print array value we use-
   javascript_array_variable=["zero", "one", "two"];
    window.alert(javascript_array_variable [0]);

   Output Value- Zero
 
array in javascript work same as in other language like java, c.


An object is a group of values;
unlike arrays, we can do something better than them:
myObject = {};
sanddep = {
                       firstname: "Sandeep",
                       lastname: "Chauhan",
                       fullname: "Sandeep Chauhan"
           };
gurjar = {   
                 firstname: "amit",   
                 lastname: "gurjar", 
                 fullname: "amit gurjar"
             };
 
 window.alert(sandeep.fullname);   
//Output- John Doe
window.alert(gurjar.firstname);
// Billy
Rather than making an array ["Sanddep gurjar", "Sandeep"] and calling myArray[0],
we can just call sandeep.fullname and gurjar.fullname

No comments:

Post a Comment

Translate