Van icon

JAVASCRIPT FUNCTIONS

Links


    //REUSABLE FUNCTION
    function ourReusableFunction(){
        console.log("All things must pass");
    }
    ourReusableFunction()

    //FUNCTIONS WITH ARGUMENTS
    function ourFunctionWithArgs(a,b){
        console.log(a - b);
    }
    console.log("Function with arguments: 10-3");
    ourFunctionWithArgs(10, 3);

    



    //SCOPE
    //GLOBAL SCOPE
    var myGlobal = 10;

    function fun1() {
       oopsGlobal = 5;
    //    if it is var, it is not available in other functions
    //    empty it is available
    }

    function fun2(){
        var output = "";
        if (typeof myGlobal != "undefined"){
            output += 'myGlobal: ' + myGlobal; 
        }
        if (typeof oopsGlobal != "undefined"){
            output += " oopsGlobal: " + oopsGlobal;
        }
        console.log(output);
    }
    console.log("Global Scope:")
    fun1();
    fun2();


    //LOCAL SCOPE - only visible in the function
    function myLocalScope(){
        var myVar = 5;
        console.log(myVar);
    }
    console.log("Local Scope:")
    myLocalScope();
    // console.log(myVar); will give error



    //FUNCTION WITH RETURN
    function minusSeven(num){
        return num - 7;
    }
    console.log("Function with return:")
    console.log(minusSeven(10));

    //UNDEFINED VALUE RETURNED FROM A FUNCTION
    var sum = 0;
    function addThree(){
        sum = sum + 3;
    }
    function addFive(){
        sum += sum + 5;
    }

    //ASSIGNEMENT WITH A RETURNED VALUE
    var changed = 0;
    function change(num){
        return (num + 5) / 3;
    }
    changed = change(10);



    //STAND IN LINE
    function nextInLine(arr, item){
        arr.push(item);
        return arr.shift();
    }
    var testArr = [1,2,3,4,5];

    console.log("Before: "+ JSON.stringify(testArr));
    console.log(nextInLine(testArr, 6));
    console.log("After: "+ JSON.stringify(testArr));



    //BOOLEAN VALUES
    function welcomeToBooleans() {
        return false;
    }
    console.log("Boolean Values: ")
    console.log(welcomeToBooleans());



    //BOOLEAN VALUES
    function welcomeToBooleans() {
        return false;
    }
    console.log("Boolean Values: ")
    console.log(welcomeToBooleans());



    //BOOLEAN VALUES
    parseInt();
    isNan();