JavaScript How To Call A Function

Raja Tamil
1 min readOct 10, 2022

Learn how to call a function in JavaScript.

This is a two step process:

  1. Declare a function.
  2. Call it.

There are two ways to declare a function.

  1. Using Function Declaration
  2. Using Function Expression

Declare A Function Using Function Declaration

Add the function keyword in front of the function name when declaring a function using Function Declaration.

These type of functions are hoisted and become global.

That means the function will be pushed all the way up its scope regardless of where its declared in the scope.

And it will be available anywhere in that scope.

For that reason, you can call or initialize a function before declaring it.

// Call a function
sayHello(); // it will work even if its called before the function declaration
// Declare A Function
function sayHello() {
console.log("Hello");
}

Declare A Function Using Function Expression

In Function Expression, you define an anonymous function and assign it to a variable.

When you use Function Expression to create a function, you must declare it first before calling it.

--

--

Raja Tamil

👇click the link and get 80% OFF on the premium web development courses (HTML5, CSS3, JavaScript ES6, Vue JS 3 & Firebase 10 https://softauthor.com