1. Function

    They do certain work and can be called many times.

    function nameOfFunction (parameters) {
    	main body
    }
    
    function greet () {
        console.log("Good Morning");
    }
    greet();
    

    Local Variables

    variables declared inside the function which can only be visible inside the function

    Global Variables

    variables declared outside the function. All the function can use this variable

    Parameters

    The values which we have to later pass into a function is called parameter.

    function findArea (length, breadth) {
        console.log(length * breadth);
    }
    
    findArea(2,3);
    

    Argument

    When a value is passed as a function parameter, it is called argument

    How to give default values to parameters

    function findArea (length = 5, breadth = 7) {
        console.log(length * breadth);
    }
    
    findArea();
    

    Returning a value - VIP

    function findArea (length, breadth) {
        return length * breadth;
    }
    
    console.log(findArea(3,4));
    

    If we only return; then the function exit immediately and return undefined and all the code below that wont execute

    Never Add a new line between return and value because it will return undefined.

    While naming a function, since functions are actions their name is usually verb. But for arrow functions which return variables must be noun.

    While making function we must follow rule of one function - one action. If there are two actions, we need two functions. It is better for industry practice. Thus function should be short and exactly do one thing, if it is big may be we should split into few smaller functions. This simple practice can make you a good developer later.

  2. Function expression

    It is creating a function and assigning it to a variable.

    const greet = function () {
        return "Good Morning";
    }
    
    console.log(greet());
    

    Task - Find the volume of a cuboid using function expression. V= l * b * h

  3. Arrow Functions - VIP - most important for react

    It is cleaner and better than function expression.

    const greet = () => {
        return "Good Morning";
    }
    
    console.log(greet());
    

    If we have only one parameter we can skip the round bracket

    If we have single line main body, we can skip the curly braces as well as return.

    HomeWork -