Welcome to DavisDay.com! Enjoy Your Visit!

Modern Javascript

Creating and itterating through an array

        
            
            const fruits = ["Apples","Cherries","Raspberries"]; //Initializes an array of fruits
            
            fruits.forEach((currentFruitElement) => {
                //This will loop through the array of fruits
            }); 

        
    

The forEach method is being called on the array named 'fruits'. Each time the currentFruitElement itterates through the loop, it adds on as an element.

The Ternary Operator

        
            let score = 62;
            let result;

            if(score >= 60){
                result = "PASS";
            }else{
                result = "FAIL";
            } //An if Statement

            result = score >= 60 ? "PASS" : "FAIL"; //The Ternary Operator
        
    

The ternary operator is a shorthand way of creating an if statement. It is a truthy falsy operation, that can be used in Javascript and Excel. The first statement entered executes if it's truthy, and last statement executes if it returns falsy.

Copying an Array

        
            //Copying Arrays
            const videoGameNames = ["Elden Ring", "Wobbly Life"];
            const namesCopy = [...VideoGameNames];
        
    

Using methods such as sort, filter, and reduce will change an array. For this reason, it is handy and smart to make a back up of your arrays, incase you still need an orignal. The example will copy the array videoGameNames, so any necessary modifications can be made.