JSFiddle - React, Tailwind, and code Playground

by Tika Datta Pahadi

JavaScript

//There is a caution! Maybe you did that:
//Let us say an array and a method foo which return first element:

    var myArray = ["apple","ball","cat"];
    
    console.log(foo(myArray)); // <- 'apple'
    
    function foo(array){
        return array[0];
    }

//The above is okay because the functions are uplifted to the top during interpretation time.

//But, this DOES NOT work: (Because the prototype is not definned)

    //console.log(myArray.foo()) 
    // <- 'TypeError : foo is not defined yet '
    
    Array.prototype.foo = function(){
        return this[0];
    }
    
    console.log(myArray.foo()) 
    // 'apple'