Useful Array functions

Start from the beginning
                                        

};

function practicingArray(){

    let list1 = array.splice(0, 3, "apple", "banana", "chery");

    let list2 = array.slice(0, 2);

    let list3 = array.concat("draw", "element", "frame");

    let list4 = list3.concat(object1);

    document.writeln(list1);

    document.writeln(list2);

    document.writeln(list3);

    document.writeln(list4);

    document.writeln(array);

}

======================

HTML

practicingArray()

Result:

1,2,3 apple,banana apple,banana,chery,draw,element,frame apple,banana,chery,draw,element,frame,green,heaven,india,joker apple,banana,chery

Note: The displays are as follows: array, list2, list3, list4, list1


4. forEach(function(value, index, array){ // no return statements; })

    - you can use the forEach function to iterate throught the array. index is the key. value is the element's value. array is your array.

====================

let array = [1, 2, 3];

let object1 = {

    0: "green",

    1: "heaven",

    2: "india",

    3: "joker",

    [Symbol.isConcatSpreadable]: true,

    length: 4

};

function practicingArray(){

    let list1 = array.splice(0, 3, "apple", "banana", "chery");

    let list2 = array.slice(0, 2);

    let list3 = array.concat("draw", "element", "frame");

    let list4 = list3.concat(object1);

    list4.forEach((value, index, _) => {document.writeln('index${index}: ${value},')});

}

====================

HTML

practicingArray()

Result:

index0: apple, index1: banana, index2: chery, index3: draw, index4: element, index5: frame, index6: green, index7: heaven, index8: india, index9: joker,

5. indexOf(value)

    - it returns the index of the element where counting starts from zero to last index.

6. lastIndexOf(value)

    - it returns the index of the element where counting starts from last to zero index.

7. includes(value)

    - it returns true if the value is within the array.

8. find(function(value, index, array){ // boolean statement; })

Javascript ProgrammingWhere stories live. Discover now