The Wayback Machine - https://web.archive.org/web/20241130160720/https://www.geeksforgeeks.org/functions-in-javascript/
Open In App

Functions in JavaScript

Last Updated : 25 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A function in JavaScript is a reusable block of code that performs a specific task. You define it once, and then you can run (or “call”) it whenever you need that task done in your program.

JavaScript
function sum(x, y) { 
    return x + y; 
}
console.log(sum(6, 9));

Output
15

Function Syntax and Working

A function definition is sometimes also termed a function declaration or function statement. Below are the rules for creating a function in JavaScript:

  • Begin with the keyword function followed by,
  • A user-defined function name (In the above example, name is sum)
  • A list of parameters enclosed within parentheses and separated by commas (In the above example, parameters are x and y)
  • A list of statements composing the body of the function enclosed within curly braces {} (In the above example, statement is “return x + y”).

Return Statement

There are some situations when we want to return some values from a function after performing some operations. In such cases, we make use of the return. This is an optional statement. In the above function, “sum()” returns the sum of two as result. 

Function Parameters

Parameters are input passed to a function. In the above example, sum() takes two parameters, x and y.

Calling Functions

After defining a function, the next step is to call them to make use of the function. We can call a function by using the function name separated by the value of parameters enclosed between the parenthesis.

JavaScript
// Function Definition
function welcomeMsg(name) {
    return ("Hello " + name + " welcome to GeeksforGeeks");
}

let nameVal = "User";

// calling the function
console.log(welcomeMsg(nameVal));

Output
Hello User welcome to GeeksforGeeks

Why Functions?

  • Functions can be used multiple times, reducing redundancy.
  • Break down complex problems into manageable pieces.
  • Manage complexity by hiding implementation details.
  • Can call themselves to solve problems recursively.

Function Invocation

The function code you have written will be executed whenever it is called.

  • Triggered by an event (e.g., a button click by a user).
  • When explicitly called from JavaScript code.
  • Automatically executed, such as in self-invoking functions.

Function Expression

It is similar to a function declaration without the function name. Function expressions can be stored in a variable assignment. 

Syntax:

let geeksforGeeks= function(paramA, paramB) {
    // Set of statements
}
JavaScript
const square = function (number) {
      return number * number;
};
const x = square(4); // x gets the value 16
console.log(x);

Output
16

Arrow Function:

Arrow Function is one of the most used and efficient methods to create a function in JavaScript because of its comparatively easy implementation. It is a simplified as well as a more compact version of a regular or normal function expression or syntax.

Syntax:

let function_name = (argument1, argument2 ,..) => expression
JavaScript
const a = ["Hydrogen", "Helium", "Lithium", "Beryllium"];

const a2 = a.map(function (s) {
    return s.length;
});

console.log("Normal way ", a2); // [8, 6, 7, 9]

const a3 = a.map((s) => s.length);

console.log("Using Arrow Function ", a3); // [8, 6, 7, 9]

Output
Normal way  [ 8, 6, 7, 9 ]
Using Arrow Function  [ 8, 6, 7, 9 ]

Functions in JavaScript – FAQs

What is a function in JavaScript?

A function is a reusable block of code designed to perform a particular task. Functions can take inputs, process them, and return a result.

How do you define a function?

Functions can be defined using function declarations or function expressions.

What is a function declaration?

A function declaration defines a function with the specified parameters and code block. The function can be called before it is defined due to hoisting.

What is a function expression?

A function expression defines a function inside an expression. The function can be anonymous and is not hoisted, so it cannot be called before it is defined.

What are arrow functions?

Arrow functions provide a shorter syntax for writing functions. They do not have their own this context and are not hoisted.

Example: const name = (parameters) => { // code to be executed };

How do you call a function?

You call a function by using its name followed by parentheses, which may include arguments.



Previous Article
Next Article

Similar Reads

three90RightbarBannerImg