Functions in JavaScript

Functions in JavaScript

Discover the Power of JavaScript Functions: Understanding and Utilizing Their Benefits for Better Code

ยท

4 min read

function in programming refers to a block of code that performs a specific task, functions allow the developer to break complex problems into smaller ones or long code into smaller manageable code. Functions can be reused, so it also reduces code duplication. Functions take input, process them and return a value, a function is only executed once they are called.

Function Declaration

In JavaScript function is defined using function keyword followed by the function name.

Syntax:

function sayHello(){

// code to be executed

}

function sayHello() {
  console.log("Hello");
}

in the above example, we have declared a sayHello function, for any function to be executed we need to call that function.

This is how we can call that sayHello function*.*

// when we call the sayHello function Hello message will be displayed on the screen.
sayHello(); // -> Hello

Declaring another function with a parameter

We can also declare functions with parameters. A parameter in a function is a variable that is used to accept an argument when the function is called. It stores the arguments passed to the function so that its value can be used later. This is how we can declare a function in javascript with parameters.

// function declaration with parameter
function sayHello(name) {
  console.log(`Hello ${name}`);
}

// calling the function with an argument.
sayHello("Ankit"); // -> Hello Ankit

In the above example, we have used the same sayHello function, but this time it is declared with name parameter. When we call this function we need to pass an argument and the value of the argument will be stored inside the name and we can use it later. Here we are displaying a Hello message with the name passed as an argument.

parameter and arugment.

A parameter is a variable that is declared in the function definition. It acts as a placeholder for the arguments that will be passed to the function when it is called. The parameter is used to receive the value of the argument within the function.

An argument, on the other hand, is the actual value passed to the function when it is called. The argument provides the input to the function, which is then stored in the corresponding parameter.

In the above example, while declaring the function name is the parameter and while calling the function we passed "Ankit" that is the arguments.

A function can be declared with any number of parameters.

Returning a value from a function.

functions can also be used to return some values, return keyword is used to return some values from a function. when a function encounters the return statement then the execution of the function is stopped, and any code after return statement is not executed. Take a look at the example below.

function square(number) {
  return number * number;
}

// calling the sqare function with 5 as argument and value function will return will be stored in the output variable.
const output = square(5);
console.log(output);

In the above example, we have declared a square function that accepts a number as an argument and returns the square of the number. next, we are calling the square function with 5 as an argument, and the value the function will return will be stored in the output variable later we are displaying the output in the console.

Note:

Function in javascript is hoisted, in short, it means that we can call a function before declaring them.

Function Expression

In Javascript we can also define functions as values and store them in a variable, this is known as a function expression. remember that function expression is not hoisted which means you can't use function expression before you create them.

const square = function (number) {
  return number * number;
};

const output = square(5);
console.log(output);

Arrow function

Arrow functions were introduced in ES6. The Arrow function allows us to declare functions with shorter syntax. This is how we declare the arrow function.

const square = (number) => {
  return number * number;
};

const output = square(5);
console.log(output);

Conclusion ๐Ÿ™‹โ€โ™€๏ธ๐Ÿ™‹โ€โ™‚๏ธ

That's all for this article, functions are a very important concept of any programming language, and knowledge about functions is definitely going to help you to become a better developer.

Did you find this article valuable?

Support Ankit Kumar by becoming a sponsor. Any amount is appreciated!