Functions - Javascript

  • Creating a function
        function variable (parameters) {
            // Statements
            return value;
        }
        // Give parameter a default value
        function variable(parameters = value) {
            // Statements
        }
        // If only one argument & statement, Automatically return
        const variable = parameter => statement
        // Arrow Function
        const variable = (parameters) => {
            // Statements
        }
        const variable = function(parameters) {
            // Statements
        }
        variable(arguments) // Call a function
    
  • this.var
    • Points to Object of the function it is inside of
    • In case of arrow function it will find global window object if necessary or lexical this
    • In case of events, it will refer to the element that receives the event
  • Scope
    • Global
      • Available in every file loaded after it
    • Module
      • Only available within that file unless you explicitly export it
    • Function
      • var is available inside the whole function not just in block level
    • Block
      • Available only inside the curly braces
      • let & const have block level scope
    • Scope chain
      • Stack of currently accessible scopes, from the most immediate context to the global context
  • Immediately Invoked Function Expressions (IIFE)
        (function () {
            // Statements
        })();
        variable(A).next().value
    
  • Closure
    • A function has access to the variable environment (VE) of the execution context in which it was created
    • Closure is VE attached to the function
    • Code
          const functionName = function () {
              let varName1 = 0;
              return function () {
                  varName1++;
              }
          }
          const varName2 = functionName();
          varName2();
      
  • Call back Function => Function inside a function
  • Iterator
    • Code
          function functionName(A) {
              let var = 0;
              return {
                  next: function () {
                      if (var < A.length) {
                          return {
                              value: A[var++], done: false
                          }
                      }
                      else {
                          return {
                              done: true
                          }
                      }
                  }
              }
          }
          // Gives the next value of elements of array after each execution
          functionName(A).next().value
      
    • Generator
      • The yield keyword pauses the generator function execution, and the value of the expression following the yield keyword is returned to the generator's caller, Returns a object with value & done just like Iterator
      • Code
            function* variable() {
                yield value1;
                yield value2;
            }
            variable(A).next().value // Gives the naxt value of elements of array after each execution
        
Share: