JavaScript - ES6 methods

revision:


ES6 methods list and description

Array.from() : converts array-like values and iterable values into arrays.

Array.of() : creates an instance from a variable number of arguments instead of the number of arguments or type of arguments.

string.includes() : returns true if a string contains a specified value, otherwise false.

copyWithin() : copies the part of an array to a different location within the same array.

find() : finds a value from an array, based on the specific criteria that are passed to this method.

findIndex() : returns the index of the first element of the given array that satisfies the given condition.

entries() : returns an array iterator object, which can be used to loop through keys and values of arrays.

keys() : returns an array iterator object along with the keys of the array.

values() : provides the value of each key.

fill() : fills the specified array elements with a static value.

string.startsWith() : returns true if a string begins with a specified value, otherwise false.

string.endsWith() : returns true if a string ends with a specified value, otherwise false.

Number.isInteger() : returns true if the argument is an integer.

Number.isSafeNumber() : returns true if the argument is a safe integer. A safe integer is an integer that can be exactly represented as a double precision number.

isFinite() : returns false if the argument is Infinity or NaN. Otherwise it returns true.

isNaN() : returns true if the argument is NaN. Otherwise it returns false.

spread (...) operator : expands an iterable (like an array) into more element.


examples

code:
                <div>
                    <p id="ES6_1"></p>
                    <p id="ES6_2"></p>
                    <p id="ES6_3"></p>
                    <p id="ES6_4"></p>
                    <p id="ES6_5"></p>
                    <p id="ES6_6"></p>
                    <p id="ES6_7"></p>
                    <p id="ES6_8"></p>
                    <p id="ES6_9"></p>
                    <p id="ES6_10"></p>
                    <p id="ES6_11"></p>
                    <p id="ES6_12"></p>
        
                </div>
                <script>
                    var arr=[2.1,3.5,4.7]; 
                    var result = arr.map((numA) => 2*numA );  
                    console.log(result);
                    document.getElementById("ES6_1").innerHTML = "map(): " + result;
        
                    const ages = [32, 33, 16, 40];
                    function checkAge(age) {
                         return age > 18;
                    }
                    document.getElementById("ES6_2").innerHTML = "every(): " + (ages.every(checkAge));
                    console.log(ages.every(checkAge))
        
                    const ages_A = [32, 33, 16, 40];
                    console.log(ages_A.includes(33))
                    document.getElementById("ES6_3").innerHTML = "includes(): " + ages_A.includes(33);
        
                    const ages_B = [33, 32, 16];
                    for (const element of ages_B) {
                        console.log("for ... of iterator:" + (element + "b"));
                        document.getElementById("ES6_4").innerHTML += "for . . . of operator: " + (element + "b") +"<br>" ;
                    }
        
                    const ages_C = [33, 32, 16];
                    console.log("spread operator: " + ages_C);
                    document.getElementById("ES6_5").innerHTML = "spread operator: " + ages_C;
                  
                    const ages_D = [33, 32, 16];
                    console.log("filter(): " + ages_D.filter((age) => age>20));
                    document.getElementById("ES6_6").innerHTML = "filter(): " + ages_D.filter((age)=> age>20);
        
                    const ages_E = [33, 32, 16];
                    const reducer = (first, second) => first + second;
                    console.log("reduce(): " + ages_D.reduce(reducer));
                    document.getElementById("ES6_7").innerHTML = "reduce(): " + ages_D.reduce(reducer);
        
                    const myArr = Array.from("ABCDEFG");
                    document.getElementById("ES6_8").innerHTML = "Array.from() : " + myArr;
                    
                    const numbers = [4, 9, 16, 25, 29];
                    let first = numbers.find(findFunction);
                    document.getElementById("ES6_9").innerHTML = "first number over 18 is " + first;
                    document.getElementById("ES6_10").innerHTML = "first number over 18 has index " + 
                    numbers.findIndex(findFunction);numbers.findIndex(findFunction);
                    function findFunction(value, index, array) {
                        return value > 18;
                    }
                    document.getElementById("ES6_11").innerHTML =  " is 10 an integer, is 10.5 an integer? : " 
                    + Number.isInteger(10) + " , " + Number.isInteger(10.5);
                    document.getElementById("ES6_12").innerHTML =  " is 10 a safe integer, is 12345678901234567890
                     a safe integer? : " + Number.isSafeInteger(10) + " , " + Number.isSafeInteger(12345678901234567890);
                    
                </script>