JavaScript Functions


A function is a collection of statements that accept input, do a specified computation, and return output. The goal is to combine certain frequently or repeatedly performed activities into a function, so that instead of writing the same code for different inputs, we may use that function.

Example

Preview

 JavaScript has support for functions. You've probably seen some commonly used JavaScript functions, such as alert(), which is a built-in JavaScript function. However, JavaScript also allows us to create user-defined functions. Using the keyword function, we can create functions in JavaScript.

To build a function in JavaScript, we must first use the keyword function, followed by the function name and parameters enclosed in parentheses. The body of the function is the component of the function enclosed by curly brackets. Functions in JavaScript can be used for assignments or computations in the same manner that variables can.

Syntax


1. Function Definition

We must first construct a user-defined function in JavaScript before we can utilize it. In JavaScript, we may use the aforementioned syntax to define a function. A function definition is also known as a function declaration or a function statement. The following are the guidelines for writing a function in JavaScript:

Every function should start with the term function, then:

  • A user-defined function name that is distinct,
  • A set of parameters separated by commas and contained in parenthesis,
  • A list of statements composing the function's body enclosed in curly braces{ }.

Example

Preview

Function Declaration

It uses the function keyword to declare a function. A function name must be specified in the function declaration.

Example

Preview

Function Expression

Function Expressions are similar to function declarations but do not include the function name. Variable assignments can be used to hold function expressions.

Example

Preview

Arrow Function

Because of its simplicity, the arrow function is one of the most commonly used and efficient techniques for creating a function in JavaScript. It is a more condensed and simplified variant of a regular or conventional function expression or syntax.

Example

Preview

2. Function Parameters

We've heard a lot about function parameters but haven't gone through them in depth. Parameters are pieces of information that are passed to a function. In the above example, the function calcAddition's duty is to compute the addition of two integers. The two integers on which we wish to conduct the addition operation are supplied as inputs to this function.

Example

Preview

The parameters are enclosed in parentheses after the function name and separated by commas. A JavaScript function can have an unlimited number of parameters, but it cannot have a single parameter.


Calling Functions

After defining a function, the following step is to call it in order to use it. We may call a function by using the function name followed by the argument values contained in parenthesis and a semicolon at the end.


Return Statement

In certain cases, we wish to return values from a function after doing some actions. In such circumstances, we can utilize JavaScript's return statement. This is an optional statement that is usually the last one in a JavaScript function. Consider our first example, which uses the method calcAddition. This function computes two numbers and returns the result.

Example

Preview

FAQs

JavaScript functions are reusable blocks of code that perform a specific task or calculation. They allow developers to encapsulate functionality and execute it whenever needed by calling the function. Functions help organize and modularize code, making it more readable, maintainable, and reusable. They can accept input parameters, perform operations, and return results. JavaScript functions play a vital role in building complex applications by breaking down the code into smaller, manageable units that can be reused and composed to create more powerful functionality.

In JavaScript, you can declare a function using the function keyword, followed by the function name and parentheses that may contain parameters. For example, function greet(name) { ... } declares a function called greet with a name parameter. The function's code block, known as the function body, is enclosed within curly braces { ... } and contains the statements that define the behavior of the function. To execute the function, you call it by using the function name followed by parentheses and passing any necessary arguments. For example, greet("John") calls the greet function with the argument "John".

Yes, JavaScript functions can return values using the return keyword. When a function reaches a return statement, it immediately stops executing and returns the specified value. The returned value can be assigned to a variable or used directly in an expression. If a function does not have a return statement or the return statement is omitted, the function will return undefined. By returning values, functions enable the passing of data back to the calling code, allowing for further processing or utilization of the function's result in other parts of the program.