JavaScript Functions


JavaScript Function

JavaScript functions are defined with the function keyword.

You can use a function declaration or a function expression to define a function.There is an another way to define a function, called
Arrow Function

Function Declarations
You can declare a function using this following syntax

Syntax::

        function myFunction(){
        
        // document.write( "this is javascript"  );
        console.log("myFunction");
        }

        myFunction();




Function Expressions

A Function expression can be stored in a variable,
When you create a function and assign it to a variable, known as function expression.

Ex:
  
    var myFun = function show(){
       document.write ("this is function with expressions");
    }

    myFun();


    <script>

        var abc = function(){
            console.log("this is arrow function")
        }

        // abc();

        var sum =abc;
        console.log(sum);

    </script>

Note:

  • You can`t call function expression before function definition
  • Function expressions in JavaScript are not hosted, unlike function declaration.

Function Declaration & Expression         <script>

        // say("This is expression")    - Housting..
        write("this is declaration");

        //declaration
       function write(x){              - No arrugements
        console.log(x)
       }

      // expression                    - we can pass arrugements (parameters)
      var say=  function(x){
        console.log(x)
       }

    </script>

Arrow Function

Arrow function allows a short syntax for writing function expressions.
You don`t need the function keyword, the return keyword, and the curly brackets.

An arrow function expression (previously, and now incorrectly known as fat arrow function) has a shorter syntax compared to function expressions. Arrow functions are always anonymous.

    
     var myfun = () => {
            document.write("Hellow javascript");
         };
         myfun();


Function Parameters & Arguments


If you want to build a dynamic function then you have to use parameters. Parameters are like an input. Based on your input it gives you the output.

  • JavaScript function definitions do not specify data types for parameters.
  • JavaScript functions do not perform type checking on the passed arguments.
  • JavaScript function do not check the number of arguments received.
  • WHEN U R ASSIGNING MULTIPUL Parameters in a function is called as a ARGUMENTS
    <script>

        // function with parameters
        function abc(name,age){
            // console.log("this is ramesh");
            console.log("Hellow"+""+name+""+age)
        }

        abc("Kishore","99");
        abc("ramesh","79");
        abc("Kishore","99");
        abc("ramesh","79");

    </script>
   

 JavaScript return Statement

Definition and Usage

The return statement stops the execution of a function and returns a value from that function.

    <script>
       
        //function returns the value
        function abc(fvalue,svalue){
        //   console.log(fvalue*svalue);
        return fvalue*svalue
        }

        var values = abc(9999,7999);
        console.log(values);

    </script>

    Here you can return the value inside the arrow function also,
     here is the example...

    <script>        

        var abc = function(fvalue,svalue){
        //   console.log(fvalue*svalue);
        return fvalue*svalue;
        }

        var suresh = abc(499,799);
        console.log(suresh);

    </script>

    Variables: It holds the data or information which can be changed anytime. JavaScript use reserved keyword var to declare variables. In JavaScript, there are two types of variable and also it tells you where in your program you are allowed to use the variables and functions that you’ve defined.

  • Local Variable :

    When you use JavaScript, local variables are variables that are defined within functions. They have local scope, which means that they can only be used within the functions that define them.

    Example

        <script>

            function gvar(){
                var a = "code guruva";

                document.write(a + "<br>");
            }

            gvar();
            document.write(a);

        </script>

  • Global Variable:

    In contrast, global variables are variables that are defined outside of functions. These variables have global scope, so they can be used by any function without passing them to the function as parameters.

    Example

        <script>

            var a = "code guruva";

            function gvar(){
                document.write(a + "<br>");
            }

            gvar();
            document.write(a);

        </script>