DEV Community

Cover image for Functions in JavaScript.
Keshav Jindal
Keshav Jindal

Posted on

4

Functions in JavaScript.

1. Introduction
Function is a block of code which perform specific task in a better and efficient way.

Syntax
It is defined using function keyword, then followed by the name of the function say var1, followed with parentheses ().
Function name follows same rules as of variable name.

function var1(par1, par2) {
code to be executed
}
Enter fullscreen mode Exit fullscreen mode

2. Uses of Functions
Functions basically resist the repetition of same code again and again. Once the code is designed, we can use it multiple times just by giving the
arguments.

Example:

function add(num1, num2) {
  const addition = num1 + num2;
  console.log(addition);
}
add(2, 1);
add(63,232):
Enter fullscreen mode Exit fullscreen mode

Output:
3
295
3. Function Return
When return keyword is used in Function and it reaches to return statement, the code will stop executing. Return keyword helps in future use of the output of Function.

For example:

function multi(par1, par2) {
  return par1 * par2;
}
console.log(multi(10, 2));
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (2)

Collapse
 
vishwastyagi profile image
Vishwas Tyagi

A short and simple explanation👍

Collapse
 
zan profile image
Zan

I knew all of this but still. Would love to read more short tutorials to refresh my knowledge even about topics I know all about.

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay