5 function

  • Return value: the result of the function execution
    • usereturnTo set the return value of the function, which can be received by a variable
    • The code after the return will not execute, and the function will exit as soon as the return statement is executed
    • A return can be followed by a value of any type, primitive data type, or object
    • If return is not followed by a value, or if return is not written, the function returns undefined by default
  • Parameters:
    • Function (1, 2)
    • Argument: function(a, b)
  • Scope: The scope of a variable
    • Global scope
      • The global scope is created when the page is opened and destroyed when the page is closed
      • The global scope has a global object called Window, which is provided by the browser
      • Variables and functions created in the global scope can be accessed anywhere on the page
        • Try not to create variables globally
    • Function scope
      • A new function scope is created each time a function is called
      • A function scope is created when the function is executed and destroyed when the function is finished
      • Variables created in function scope are not globally accessible
      • When a variable is used in a function scope, it looks in its own scope first, if it finds it, it uses it directly, and if it doesn’t find it, it looks in the upper scope
  • Function declaration ahead: Like variable declaration ahead, function declaration is used in global scopefunctionThe created function is pre-parsed by the browser to move the declaration forward.