Functions in JS

sushant sareen
3 min readAug 24, 2020

--

In JavaScript, we have got four major things to do:-

  1. To Store values.
  2. To Store a series of steps.
  3. To make decisions.
  4. To Repeat the steps. (loops)

In this article, we will mainly focus on “storing a series of steps”. And to do that we need to learn about function because, in js, function helps us to store a series of steps and execute those steps/codes whenever required by calling that function.

Photo by 🇨🇭 Claudio Schwarz | @purzlbaum on Unsplash

Now coming to the syntax of the function, we have got several ways to define a function but the most basic way is to “declare a function” which is termed as function declaration.

The snippet above shows the most basic way to declare a function.

So, this is the first step in writing a function and the next step is to “call the functionand to do that we need to just write the name of the function i.e “add” and put the parenthesis (()) after that.

With that, we have seen how to declare a function and then “give a call to that”. One more important thing to note here is that we can call this function anywhere we want, like whenever we call this function we will get the code executed present between the curly braces.

Types of functions

  1. function expression

Above we have seen the most basic type of function which is function declaration. Now we will see the second type i.e. function expression.

So, to define a variable using let, var or const we need these keywords, then the name of the variable and then the assignment operator and then the value. Like this:-

let user = "sushant";

Now, to the right of assignment operator(=) we can only have string, number, boolean, null, undefined and object and any value other than that will be treated as an invalid value.

So, in JS, functions are treated as an object. So, we can define it to the right of assignment operator like this:-

let user = function user() {
return "riya";
}
user();

So, this is known as a function expression.

2. Anonymous function expression

An anonymous function expression is almost similar to function expression, the only difference is that, in this, we omit the name of the function, like this:-

let user = function () {  //name 'user' is omitted
return "riya";
}
user();

3. Arrow function

For an arrow function, we can say that it’s a kind of a short form of anonymous function expression. In writing arrow function, we omit the word function from it and just use parenthesis and after parenthesis add the symbol “=>”, like this:-

let user = () => {
return "riya";
}
user();

Now, if there is only one line in curly braces then we can omit these curly braces and the keyword “return” too:-

let user = () => "riya";user();

Function with arguments

Now that we have discussed the most basic part of functions and almost all types of functions, we will learn about function with arguments.

Sometimes what happens is that we need to get different “return” value but the steps should be the same. For example:- we need to do the same operation, say multiplication, but with different operands, in that case we need to write the function again and again. So, here we use functions with arguments, Like this: -

function mul(a, b) {
return a * b;
};
console.log(mul(20, 30));
console.log(mul(10, 15));

Hence, in this case, we don’t need to write the code, again and again, we just need to change the arguments & we will get the result.

So, that was all about functions with arguments.

Two most important concepts of the basics of functions are left, that is, about “return” keyword and “function with default parameter”. We may discuss these in future.

Thank you.

--

--