Functions are essential part of our daily code an everybody uses them at one point in time.
Here is a basic skeleton of a javascript function:
function functionName(){
//code goes here;
}
let us explore a little more sophisticated concepts on functions.
1.Immediately invoked function expressions (IIFE)
These means invoking a function immediately after it has been defined.
for example instead of writting:
function helloWorld(){
console.log('hello world');
}
helloWorld(); //invoking it manually
we can use the IIFE to do it immediately after defining it.
(function() {
console.log("hello world");
)();}
- Arrow functions Was introduced in ES6.It is a simpler way to write function expression. (note: it is not a substitute to writing functions)
example:
//function to return sum of two numbers
let sum = (num1 , num2) => num1 + num2;
console.log(sum(2,6)); //8
why use arrow functions?
- shorter syntax.
- simpler way to write function expression.
- this derives its value from enclosing lexical scope.
side effects
- you have to be ware of the behavior of 'this' keyword.
- no argument object.
Thanks for making time
Twitter: @kimanigeoffre14
Top comments (0)