Devtantra Blog

Tutorials about Android, Angular, ReactJS, PHP, MySQL and Web Development

JavaScript Array & Objects

Array is variable which store many values ,but in javascript you can store different type of values in it . An array same as your billing receipt.

array

Array (One,two&multi dimensional)

Declaration Array

In javascript you can declare array by this

var array name = [item1, …];

Example

var basic = ["no1", "no2", "no3"];

Using Keyword new

By the help of new keyword you can also create array like this

var array name = new array (elem1, elem2, …);
Both method is of array declaration is good ,but most beginners  use first method, because it is simple and for second method you need to know about object and etc..

Access Array

For access the data of array you need to know its Index . If you declare an array with 6 value and if you want access first value then you need to write value with index [0] . Like this

var num=[1,2,3,4,5,6];
console.log(num[0]);//output is 1

Declaration 2d array

var array name = [[item1], …];
console.log(array name[x][y]);//for output

Example

var ara =[[2], [8,1], ["He"]];
console.log(ara[0][0]);//output 2
  •  Here i declare two dimensional Array . An two dimensional array has one more array in its each index which is shown above..
  • In above syntax i define that put x and y and there x means main array index and y means second array index.
Same as two dimensional array you can create multidimensional arrray .like var seara =[[[2]], [8,1], [“Hello !”]]; console.log(seara[0][0][0]);

Object

Object is complete bunch of properties and methods.Object is very useful in programming , By the help of object reduce your code length.

Declaration of Object

In javscript you can declare object by two methods

First Method:This is a simple method

var name = {object1:value,…..};

Second Method:

function name(variable) {
this.variable = variable;
}
  • Here this is a keyword is use for calling object

Example of first method

var student = {first name :"mks";
lastname:"soni";
age:24;
};
console.log(student.age);//output is 24

Example of Second Method

function classstu(firstname, lastname, age) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.age = age;
    
}
var myfather = new classstu("suresh","soni");
myfather.age;//output is undefined
The output of this is undefined because i not defined the object value

Method Accessing

function detost(firstname, lastname, age,special) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.age = age;
    this.special=special;
}
var pankaj = new detost("pankaj", "swami",21,styles);
pankaj.firstname.toUpperCase();// ouptu id PANKAJ
  • Here i use toUpperCase() method which is in built method of  javascript

Another Example 

function detost(firstname,lastname,age) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.age = age;
    this.output = output;
    function output() {
        console.log(firstname+""+lastname+""+age);
    }
}
var pankaj = new detost("pankaj", "swami",21);
myMother.name();//output pankaj swami 21

As we know , some concept of javascript is same as java ; so in javascript inheritance method is also available but in javascript inheritance is done by using prototype . Basically prototype use for create object of any function for any other function . Let see how

function sir(){
                this.methoda= function(){
                                         console.log("methoda is inherit");                                         }
               }
function student(){
 
                 this.methodb=   function(){
                                         console.log("methodb is also working");
                                        }
                         }
student.prototype = new sir(); //Inheriting the property&method of sir
 
var obja =new sir();
var objb= new student();
  
console.log(objb.methoda());//output is of methoda
console.log(objb.methodb());//output is of methodb

Click Me>>

0
0

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: