10 important things about javascript function

Asif Ur Rahman
2 min readMay 6, 2021

--

The javascript function is a building block of some code which executed by calling it. We can return various mathematical operations by passing value through it or calling it.

Anyway function is an important part of any programming language. Because it reduces the resues of the same code.

Today I will discuss 10 important things about javascript functions.

  1. Syntax:

Normally a javascript function is defined like this-

function name(parameter1, parameter2, parameter3) {
// code to be executed
}

Where function is a keyword. Then you have to give the name of the function. Then you have to pass data through this function which is called parameters.

Inside the curly bracket, you have to push your code which will be executed.

2. Function invocation:

Function invocation means function call. It can be accomplished in 3 ways.

  • By completing any event like clicking a button.
  • By calling it outside the function.
  • By self-calling.

3. Function return:

When anyone calls a function then the function is executed and returns the value to the caller.

function multiplication(a, b) {
return a * b; // Function returns the product of a and b
}
const result= multiplication(4, 3); // Function is called, return value will end up in result. where result = 12.

4. Local variable:

Sometimes you need to contain some value in a variable that will be executed only inside a specific function. Then you have to declare it locally which can be accomplished by declaring the variable inside the function.

The interesting matter is that the local variable is not executed outside the function.

function car() {var carName = “Volvo”;// code here CAN use carName}console.log(carName); // carName is not defined

5. Functions with Default Parameter Values:

Sometimes, a function is called without passing through any arguments. In this situation, the function will return default parameters as undefined. But it can be solved in such a way where you can set a specific value of the parameters. Then if the function is called without passing any value through it then it will return the default values which you have set.

function name(FirstName, LastName) {console.log(FirstName, LastName);// undefined, undefined}name(); 

But after setting the default parameter:

function name(FirstName=”Asif Ur”, LastName =”Rahman”) {console.log(FirstName, LastName); // Asif Ur Rahman}name();

6. Anonymous Function:

An anonymous function is that which has no name. Sometimes this type of function used to store the value of the function in a variable.

let show = function () {
console.log(‘Anonymous function’); // Anonymous function
};
show();

--

--

Asif Ur Rahman
Asif Ur Rahman

Written by Asif Ur Rahman

Hi ! I am Asif. A full time web developer. || Web Developer at OmniGo || MERN Stack Developer

No responses yet