DEV Community

Cover image for Functions in JavaScript.
Keshav Jindal
Keshav Jindal

Posted on


Functions in JavaScript.

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

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


function add(num1, num2) {
  const addition = num1 + num2;
add(2, 1);
Enter fullscreen mode Exit fullscreen mode

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

Top comments (2)

vishwastyagi profile image
Vishwas Tyagi

A short and simple explanation👍

zan profile image

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.

11 Tips That Make You a Better Typescript Programmer


1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields


Read the whole post now!