logo javascript

Javascript Course for Beginners

Instructor: Ivan Lourenço Gomes

Data Types - Arrays

Arrays are ordered sequences of values. Those values can be of any data type and are separated by comma. Arrays have to be declared inside braces.

                    
    var students = [ "John" , "Mary", "Paul" ];
    var primeNumbers = [ 2, 3, 5, 7, 11, 13 ];
                    
                

Like strings, arrays also have length and their elements have their own indexes.

                    
    students = [ "John" , "Mary", "Paul" ];
    console.log(students.length); // The console will show 3
    console.log(students[0]);  // The console will show "John"

                    
                

Array elements can also be arrays and objects.

                    

    var groups = [ 
        [ "John" , "Mary" ],
        [ "Peter" , "Joana", "Andrew", "Julio" ],
        [ "Caroline" , "Helen", "Mark" ]
    ];

                    
                

This was an example of a multidimensional array. Can you guess what is the length of the groups array?

The groups array has 3 elements.

                    
    console.log(groups.length); // The console will 3
    console.log(groups[1]);  // The console will [ "Peter" , "Joana", "Andrew", "Julio" ]

    /* We can access arrays inside arrays by adding the index notation as many times as needed */

    console.log(grupos[1][2]);  // The console will "Andrew"

                    
                

Array operations

  • Push - Add elements to the end of an array
  • Pop - Remove the last element of an array
  • Shift - Remove the first element of an array
  • Unshift - Add elements to the beginning of an array
  •                     
    
        var courses = [ "HTML", "Python", "PHP" ];
    
        courses.push("Javascript");
    
        console.log(courses);  // The console will show [ "HTML", "Python", "PHP", "Javascript" ]
    
        courses.unshift("Bootstrap");
    
        console.log(courses);  // The console will show [ "Bootstrap", "HTML", "Python", "PHP", "Javascript" ]
    
        courses.pop();
    
        console.log(courses);  // The console will show [ "Bootstrap", "HTML", "Python", "PHP" ]
    
        courses.shift();
    
        console.log(courses);  // The console will show [ "HTML", "Python", "PHP" ]
    
                        
                    

    It's also possible to redefine elements using the index notation

                        
    
        var ingredients = [ "bread", "cheese", "ham" ];
    
        ingredients[0] = "whole bread";
    
        console.log(ingredients);  // The console will show [ "whole bread", "cheese", "ham" ]
    
                        
                    
  • Slice - extracts part of an array
  • The slice method extracts part of an array. We do it by choosing a start and an end index (end element not included).

                        
    
        var students = [ "Peter" , "Joana", "Andrew", "Julio", "Kate", "Marie" ];
        console.log( students.slice(0,3) );
    
        // The console will show [ "Peter" , "Joana", "Andrew" ]
    
                        
                    

    By omitting second value we go until the last element with it included.

                        
    
        var students = [ "Peter" , "Joana", "Andrew", "Julio", "Kate", "Marie" ];
        
        console.log( students.slice(3,) );
        // The console will show ["Julio", "Kate", "Marie"]
    
                        
                    

    We can also use negative numbers to start counting backwards from the end of an array. To get the last three elements of an array we do:

                        
    
        var students = [ "Peter" , "Joana", "Andrew", "Julio", "Kate", "Marie" ];
        
        console.log( students.slice(-3,) );
        // The console will show ["Julio", "Kate", "Marie"]
    
                        
                    

    Contents