top of page

Read my technology blog @ blog.shobhitsharma.net

Let's play with Functions in JavaScript

Updated: Jul 21, 2022

Generally speaking, The functions are a great building block that will reduce the amount of code you will need in your app.


Let's make it simpler:


You've created a kind of template that can be reused whenever you want to in many situations.


For example, you need to add two numbers and add 1 to the sum of those two numbers & you need to do this activity for more than 10 pairs of numbers like 2 and 3, 4 and 6, 9 and 21, etc.


Prashant says he can't do it manually, especially when writing code.


Here, we can write a function that will take two arguments as input parameters for the function, which will compute the sum of two numbers, add 1 at last, and return the result to us.


Let's see how we can do it in JavaScript.

function addTwo(num1, num2) {
    sum = (num1 + num2) + 1;
    return sum;
}

Isn't it cool?


We just need to call the function (technically, it is known as invoking the function) as


addTwo();


let's pass the arguments so that it can return the actual output

addTwo(1,4);

it will output the 6 as we defined in our function that adds two values passed by the user as input parameter and add 1 at last.


It's crazy!



bottom of page