Skip to main content

Command Palette

Search for a command to run...

Functions in JavaScript

Updated
•3 min read•View as Markdown
Functions in JavaScript
C

MERN stack developer passionate about technology and coding. Seeking opportunity to collaborate with developers working on challenging projects. I love to teach what I learn so I'm also a tech blogger. :-)

Hello, guys... Today i want you to explain the most important topic in javascript called functions. In this articles we are going to understand what are functions?, why is it so important is javascript and so on.

lets start!!!

What is Function

Functions are the building blocks of Javascript. Its a set of statements to achieve a particular task, it take some inputs and return the output. if you want to some operation many times in different positions, instead of writing the same logic many times, we can make it inside a function and call it whenever we need it, this is how function works.

Defining functions

Function declarations

The syntax of creating a function is :

 function name(params) {
    // num of statements
}

name();  //calling a function
  • we create a function using a keyword called function
  • The preferred naming convention for naming a function is camel case but start with lowercase.
  • The function name is followed by a set of opening and closing parentheses.

Example of a function

function add(a, b) {
    return a + b;

}
console.log(add(1, 2));

Function expressions

JavaScript is insanely popular because of its features, we can even store the function into a variables. function expression s are the other way of defining a function, its like creating a function and assigning it to a variable

lets see how it looks

var add = function (a, b) {
    return a + b;

}
console.log(add(1, 2));

Calling a function

As we have seen before, after declaring a function, we can call any times and any where just by its name and the passing the number of arguments that it has.

For the Above code

add(1, 2);   //calling a function

Parameters and Arguments

Sometimes people get confused about what are arguments and what are Parameters, actually its simple, they are almost same but having different name based on where they are using.

lets see a small example:

var subtract = function (a, b) {   //a and b are parameters
    console.log(a - b);

}
subtract(5, 3); //5 and 3 are arguments

Parameters are variables listed as a part of the function definition. Arguments are values passed to the function when it is invoked.

As we can see in the example, a & b are the parameters which we are putting while declaration of a function. 5 & 3 are the values we are passing while calling the function

Arrow functions

An arrow function expression has a shorter syntax compared to function expressions and does not have its own this, arguments, super, or new.target. Arrow functions are always anonymous.

Two factors influenced the introduction of arrow functions: shorter functions and non-binding of this.

var mul = (a, b) => {
    return a * b;
}
console.log(mul(2, 3))

I hope this small article helps you to understand the concept of functions in JavaScript and what you are waiting for just kick start with functions as much as you want to reduce the number of code lines and increase the re-usability.

Thank you :)