Let's start what is function in javascript?

Function helps us to divide our code in small modules that are easy to understand and use through out our development.

Basic syntax of function.

ES5 syntax.

ES6 syntax.

Developers mostly used ES6 syntax over ES5 and I will let you now why in upcoming blog.

Let's take an example of adding two numbers.

If I want to add two numbers in function I can write it like this

This is write but it will not print anything because a function has to be called after declaration

We can call a function by this syntax

Whenever we call a function it always print 3, what if we want to add other numbers. In that case you have to use params provided to the function

Function can also return values, say you want to return the sum of two numbers, there will be a small modification in the above code.

If a function is returning a value it can be stored in variable.


There are various built in functions that we can use, here are some examples.

  • toUpperCase()
  • toLowerCase()