DEV Community

Vignesh . M
Vignesh . M

Posted on

JAVASCRIPT CL - 9

DAY -9

// In javascript is a dianamic typeing so will given any datatype in a array and size is not fixed

//example :

const arr = [10,vic,20,30,40];
Enter fullscreen mode Exit fullscreen mode

//How print the array with different method

 //first step : console.log(arr);//full arr data
        //secound : consle.log(arr[2]);//perticulor index data
Enter fullscreen mode Exit fullscreen mode

//using WHILE loop :

const arr =[20,46,66,12,33 ]
        let i = 0
        while(i<5){
            console.log(arr[i]);
            i++;
        }//output : 20,46,66,12,33

Enter fullscreen mode Exit fullscreen mode

USING FOR LOOP

 for(i = 0;i<arr.length;i++){
            console.log(arr[i]); 
        }//output : 20,46,66,12,33
Enter fullscreen mode Exit fullscreen mode

using for-in loop
for in loop will given the index

 for(let array in arr){
            console.log(array);// show the index 
            console.log(arr[array]);//show the index value
             }//output : 0 1 2 3 4 
             //output : 20,46,66,12,33
Enter fullscreen mode Exit fullscreen mode
//for of loop 
//for of will given the index value
for(let mar of arr){
    console.log(mar);

}//output:  20,46,66,12,33
Enter fullscreen mode Exit fullscreen mode

//foreach loop
//foreach is called a callbackfuction calling statement
//foreach using function

 arr.forEach(printmark);
    function printmark(arr) {
        console.log(arr);
        }
Enter fullscreen mode Exit fullscreen mode

//foreach with function
//arroy function is not a callback function .because arroy function is a similified of function only
//foreach only runs forword only not backword

arr.forEach(marks => {console.log(marks)
    });///output:  20,46,66,12,33
Enter fullscreen mode Exit fullscreen mode

Top comments (0)