2. JavaScript function

          As codes become long and more complex there is a need to make it understandable and manageable.  The best way to maintain long lines of code is the use of functions. A function allows for simple modularity in your code wherein blocks of codes are separated based on its functionality.  It also allows for this block of code to be reused, which saves programmers and code development a lot of time.  Below is a summary of what a function is:

    • it is a block of code that is designed to achieve certain tasks.
    • unlike line codes that are executed right away when encountered by an interpreter, functions execute only it is called during:

      1. when an event occurs (i.e when a user clicks a button or when a web page loads)
      2. when it is invoked or called from a JavaScript code
      3. automatically (self-invoked)
    • is “reusable”: it is defined once, then, used several times and depending on if a function accepts arguments, it may produce varied results each time it is called.

          Functions in JavaScript or in any programming/scripting language could be grouped either as a:

    1. Native functions: predefined functions in JavaScript, or
    2. Custom functions: functions that are defined by the programmer or designer.