Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Arrays and Objects in Javascript

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 49
    Comment on it

    Objects -> Javascript objects is a set of properties and methods . A method is a member of an object and their value is property . Javascript contain 4 kinds of objects :
    1) Intrinsic objects -> Array and String .
    2) Objects
    3) Host objects -> window and document .
    4) ActiveX objects .

    In Javascript object we can add and remove the methods at run time that is called Expando Property . These type of properties and methods can have any name and they can he identified by numbers like myobj.name , myobj.age etc .
    Example ->

    var obj = new Objects ( ) ;
    obj.name = " Mukesh " ;
    obj.age = 23 ;
    obj.getAge = function ( ) {
    return this.age ;
    } ;
    document.write ( obj.name ) ; // Output : Mukesh
    document.write ( obj.age ) ;  // Output : 23
    document.write ( obj.getAge ( ) ) ; // Output : 23
    

    Arrays -> In Javascript all the things act as a Object so array is also behave as a Object or we can say arrays is also a special kind of object . Both Objects and Arrays can have properties and methods . Arrays have a length property but Object do not this is the one difference between Arrays and Objects .When you assign a value to an element of an array and if the index is greater than its length like myname [ 10] = " Mukesh " ; then the length property will automatically increased and length of an array will changed .
    Example ->

    var student = new Array ( 4 ) ;
    //Adding elements 
    student[0] = " Mukesh " ;
    student[1] =  23 ;
    student[2] = " Male " ; 
    student[3] = new Date ( 2000 , 1, 1 ) ;
    document.write( " Length is : " + student.length ) ;  // Output is : 3
    //Add some Expando properties
    student.expando = " Javascript " ;
    student[ " Second Expando " ] = " Tutorial " ; 
    document.write( " New Length is : " + student.length  ) ; // Output is : 3
    

    Multi - Dimensional Arrays
    In Javascript multi dimensional arrays is different to other languages , it does not support directly multi dimensional array in javascript but you can get the behaviour of multi dimensional arrays by storing arrays within the elements of another array . For example

    var size = 3 ;
    var a , b ;
    var multiply = new Array ( size + 1 ) ; //set the length of the array to size + 1 and first index  would be 1 not 0
    // Create the columns in the table
    for ( i =1 ; i < = size ; i++ ) {
    Multiple[i] = new Array ( size + 1 ) ;
    // Fill the row after the multiplication
    for ( j =1 ; j < = size ; j++)
    {
    Multiply[ i ][ j ] = i * j ;
    }
    }
    document.write ( Multiply[ 2 ][ 3 ] ) ; // Output -> 6
    document.write ( Multiply[ 4 ][ 5 ] ) ; // Output -> 20
    document.write ( Multiply[ 1 ][ 8 ] ) ; // Output -> 8
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: