DEV Community

kimani karaba🇰🇪
kimani karaba🇰🇪

Posted on

More on Javascript functions

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");
)();}

  1. 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?

  1. shorter syntax.
  2. simpler way to write function expression.
  3. this derives its value from enclosing lexical scope.

side effects

  1. you have to be ware of the behavior of 'this' keyword.
  2. no argument object.

Thanks for making time

Twitter: @kimanigeoffre14

Top comments (0)