Functions in javascript

What is Function in Javascript?

A function is a block of code that performs a specific task and can be called or invoked multiple times throughout a program. Functions are used to break down complex problems into smaller, more manageable parts, making the code more modular, reusable, and easier to maintain.

In JavaScript, functions are considered first-class citizens, meaning they can be assigned to variables, passed as arguments to other functions, and returned as values from functions. This flexibility and versatility of functions make them a powerful feature of the JavaScript language.

Lets understand this in simple language with real-life example :

Imagine you are making a peanut butter and jelly sandwich. You have bread, peanut butter, jelly, and a knife. To make the sandwich, you need to perform a series of steps in a specific order :

  1. Take two slices of bread and place them on a plate.

  2. Spread peanut butter on one slice of bread.

  3. Spread jelly on the other slice of bread.

  4. Put the two slices of bread together, with the peanut butter and jelly sides facing each other.

  5. Cut the sandwich in half diagonally.

Now, you could perform each of these steps manually every time you want to make a sandwich, but that would be time-consuming and tedious. Instead, you can create a function that performs these steps for you. Here's an example of what that function might look like in JavaScript:

function makePBJ() {
  console.log("Take two slices of bread and place them on a plate.");
  console.log("Spread peanut butter on one slice of bread.");
  console.log("Spread jelly on the other slice of bread.");
  console.log("Put the two slices of bread together, with the peanut butter and jelly sides facing each other.");
  console.log("Cut the sandwich in half diagonally.");

With this function, you can now make a peanut butter and jelly sandwich by simply calling the function:


This will execute all the steps in the function and make a sandwich for you.

Functions in programming work in the same way. You can define a function to perform a specific task or series of tasks, and then call that function whenever you need to perform that task again. Functions help make your code more organized, easier to read, and more reusable.

2.Creating Functions

There are several ways to create functions in JavaScript. Here are the most common ones:

  1. Function declaration: This is when you create a function with a name using the function keyword. You write the function name, followed by parentheses containing any parameters, and then a set of curly braces containing the code that the function executes. Here's an example:
function sayHello(name) {
  console.log("Hello " + name);
  1. Function expression: This is when you create a function and assign it to a variable. You use the function keyword and write the function code as usual, but instead of giving the function a name, you assign it to a variable. Here's an example:
let sayHello = function(name) {
  console.log("Hello " + name);
  1. Arrow function expression: This is a shorthand way of writing a function expression using the => syntax. It's shorter than a regular function expression and is often used for simpler functions. Here's an example:
let sayHello = (name) => {
  console.log("Hello " + name);
  1. Function constructor: This is a less common way of creating a function using the Function() constructor. It's less popular because it can be less efficient and harder to read. Here's an example:
let sayHello = new Function('name', 'console.log("Hello " + name)');

Each of these methods has its own benefits and drawbacks, and the choice of which method to use depends on the specific use case. But by understanding the basic concepts of each method, you can choose the right method for your needs.

3.Passing Arguments

When you call a function in JavaScript, you can pass values or variables as inputs to the function. These inputs are called arguments. The function uses these arguments to perform some operation and returns a result.

For example, let's say you have a function that adds two numbers:

function addNumbers(num1, num2) {
  return num1 + num2;

In this function, num1 and num2 are the parameters that represent the values that will be passed as arguments when the function is called.

To call this function and pass arguments to it, you would do it like this:

let result = addNumbers(2, 3);
console.log(result); // Output: 5

In this example, 2 and 3 are the arguments that are being passed to the function addNumbers(). The function takes these arguments, adds them together, and returns the result, which is 5.

You can pass any number of arguments to a function, depending on the number of parameters it expects. You can also pass variables as arguments, as long as they have values that match the data type of the corresponding parameters in the function signature.

Passing arguments to a function allows you to write code that can take in different inputs and perform the same operations on them. It is a fundamental concept in programming and is used extensively in JavaScript and other programming languages.

4.Returning Values

A JavaScript function can perform a task and return a value. When a function returns a value, it means that it has finished its work and sends back a result to the place where it was called from. This returned value can be used in further operations or stored in a variable for later use.

Here's an example function that adds two numbers and returns the result:

function addNumbers(num1, num2) {
  return num1 + num2;

In this function, num1 and num2 are the parameters that represent the values that will be passed as arguments when the function is called. The return statement sends back the result of adding num1 and num2.

To use this function and get the returned value, you can call the function and assign the result to a variable like this:

let sum = addNumbers(2, 3);
console.log(sum); // Output: 5

In this example, the addNumbers() function is called with arguments 2 and 3. The result of adding these numbers is returned from the function and stored in the variable sum, which is then logged to the console.

Returning values from a function is a useful feature in JavaScript that allows you to perform specific tasks and reuse code in different parts of your program. By understanding how to use the return keyword, you can create more complex and organized programs that are easier to maintain and debug.

5.Anonymous Functions

In JavaScript, an anonymous function is a function that is defined without a name. Anonymous functions are typically created as function expressions, which are assigned to a variable or passed as an argument to another function. Because they have no name, anonymous functions cannot be called directly, but can be executed through the variable or function they are assigned to.

Here's an example of an anonymous function being assigned to a variable:

let addNumbers = function(x, y) {
  return x + y;

In this example, the anonymous function is created as a function expression and assigned to the variable addNumbers. The function takes two parameters, x and y, and returns their sum. The variable addNumbers can now be used to execute the function:

let result = addNumbers(3, 5);
console.log(result); // Output: 8

This code calls the addNumbers function through the addNumbers variable, passing in the arguments 3 and 5. The function executes and returns the sum of the two values, which is assigned to the result variable and printed to the console.

Anonymous functions are commonly used in JavaScript for a variety of tasks, such as passing them as arguments to higher-order functions like setTimeout() and addEventListener(), as well as for creating immediately invoked function expressions (IIFEs) to encapsulate code and create private scopes.


Functions are an essential part of JavaScript programming. They allow you to write reusable code, encapsulate logic, and build complex applications. By mastering the basics of functions, you’ll be well on your way to becoming a proficient JavaScript developer.