In this article, we will learn about functions in JavaScript with the help of examples.
A function is a block that is designed to perform a specific task. JavaScript function executed when someone invokes it. Let’s see the syntax of the function.
Syntax of function:
// Syntax of function function functionName(){ // function body }
- In JavaScript, we declare functions using the
function
keyword. - The basic rules for naming a function are similar to naming a variable. It’s better to give your function a meaningful name. For example, if a function is used to add two numbers, you might name the function add or addNumbers.
- The body of the function is written in {}.
For example:
// Example of function // Print Recursive Minds using function function printMsg(){ console.log("Recursive Minds"); }
Invocation(calling) of function:
In the above program, we have created a function named printMsg
. To use that function, we need to call it.
Here’s how you can call the above printMsg()
function.
// function calling printMsg();
Example 1: print message using function
// Example of function // Print "Recursive Minds" using function function printMsg(){ console.log("Recursive Minds"); } // function calling printMsg();
Output:
Recursive Minds
function Parameters:
In JavaScript, we can also declare functions with parameters, A parameter is a value that is passed to a function when the function is created.
Example 2: functions with parameter
// Example of function with parameter function printMsg(name){ console.log("Recursive Minds is started by " + name); } let name = "Amit Kumar"; // Calling of function printMsg(name); // we are passing name to the function
Output:
Recursive Minds is started by Amit Kumar
In the above example, the printMsg
is declared with name
parameter. We have a name
variable and “Amit Kumar” is in the name
variable. When we called a function name
is passed as an argument into the function.
NOTE: If a value is passed when declaring a function, it is called a parameter. And when the function is called, the passed value is called an argument.
Example 3: Program to add two numbers using the function
// Program to add two numbers using a function // Function declaration function addNums(num1, num2) { console.log(num1 + num2); } // calling functions add(5,10); // Argument passing to function add(8,2);
Output:
15 10
In the above example, the addNums
function is used to find the sum of two numbers.
- The function is created with two parameters num1 and num2.
- The function is called two times using its name (addNums) and arguments are passed 5 and 10 in one and 8 and 2 in the second.
Here, you can see that we can call the function as many times as you can. You can create a function and use it multiple times with different arguments.
function return:
We can also return a value from the function and for that, we use the return
keyword inside our function. When we return a value from a function, that value is returned to the call and we can store that value in a separate variable or print that value directly.
return
denotes that function has ended. Any code after return
is not executed.
Example 4: Program to return the sum of two numbers
// program to return sum of two numbers // declaring a function function addNums(num1, num2) { return num1 + num2; } // declaration of two numbers let number1 = 50; let number2 = 20; // calling function let sum = addNums(number1,number2); // Print the sum console.log("The sum is " + sum); // We can also use console.log("The sum is " + addNums(20, 30));
Output:
70 50
function Expression:
In JavaScript, we can define the function as an expression. for example
// program to find the multiplication of two numbers // function is declared inside the variable let product = function (num1, num2) { return num1 * num2 }; console.log(product(4, 3)); // can be used as variable value for other variables let y = product(3, 6); console.log(y);
Output:
12 18
Thank you so much for reading
Happy Coding
For more details visit MDN Docs
Some other articles on JavaScript
- Introduction to JavaScript
- Variable in JavaScript
- JavaScript Data Types
- Let var const in JavaScript
- Operators in JavaScript
- JavaScript Conditionals
- for loop in JavaScript
- while and do-while in JavaScript
- break and continue in JavaScript
I wonder how much work goes into creating a website this excellent and educational. I’ve read a few really good things here, and it’s definitely worth saving for future visits.
Thanks