JavaScript Functions
Functions are one of the fundamental building blocks in JavaScript. A function is a “sub-program” that can be called by code external or internal to the function to perform a task. A function is composed of a set of statements called the function body. Values can be passed to a function and a function can return a value. A function must be called (invoked) for it to perform the specified tasks. In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object.
Define a Function
There are several ways to define a function in JavaScript.
Function Declaration
A function declaration begins with the keyword function and is followed by
· the function name,
· a list of parameters to the function, enclosed in parentheses and separated by commas,
· the JavaScript statements that define the function, enclosed in curly brackets, {...} also called function body.
Note: Function parameters are the names listed in the function's definition. Function arguments are the real values passed to the function. In the above code, a & b are the parameters whereas 60 & 120 are the arguments.
Function Expression
This form of defining a function does not require a name after the function and such a function can be called anonymous function as it does not have a name. Function are typically referenced through a variable:
The function expression is almost identical to the function declaration except for the missing name and the semicolon at the end. Another difference is that function declarations are hoisted to the top of the context when the code is executed, whereas function expressions are not hoisted.
Note: Hoisting is the process of moving a value to the top of the code block where it is used, regardless of where it is defined.
So, you can define a function after it is used in code without causing an error. Here is an example:
Trying the same thing with function expressions will result in an error:
Arrow Functions
Arrow functions are defined with a new syntax that uses an arrow (=>). The arrow comes after the list of parameters and is followed by the function’s body.
While they are like the function expressions, arrow functions have a few key differences:
If the arrow function is all on 1 line - an implicit return is added (you do not need the keyword return).
If the arrow function is on more than one line, {} must be used (just like a regular function)
If the arrow function takes 1 argument, you do not need to wrap that argument in parentheses (though for multiple arguments, you do)
Arrow functions are always anonymous
An arrow function does not have its own “this”; the “this“ value of the enclosing execution context is used.
Example without the arrow function:
In the above example, “this” is inside a declared object dog, but since the setTimeout() function is called at a later point in time, the keyword “this” does not refer to the parent object, it actually refers to the GLOBAL object which is the WINDOW object. Hence dog.sayHi() returned “Hi, I am undefined”. We lost the context of the keyword “this” in this example.
Example with arrow function:
In the above code, the arrow function keeps its value from the enclosing context. , It will refer to the “dog object”, and calling dog.sayHi() will work as expected and return “Hi, I am Oscar”.
Invoking a function
Defining a function does not execute it. Defining it simply names the function and specifies what to do when the function is called.
Invoking a function is to run the code inside the function’s body. To invoke a function, enter its name followed by parentheses – name().
Function Return Values
All functions return a value, which can be specified by the return keyword. If no return value is specified, the function will return undefined.
Summary
A function is a “sub-program” that can be called by code external or internal to the function to perform a task
They are several ways to define a function : function declaration, function expression, using arrow syntax
Function declarations are hoisted to the top of the context when the code is executed, whereas function expressions are not hoisted.
All functions return a value, which can be specified by the return keyword. If no return value is specified, the function will return undefined.
Defining a function does not execute it. To execute a function, it must be invoked
Any parameters a function needs are set when the function is defined whereas arguments are provided to the function, when in is invoked.
Thanks for reading.
References:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions











