Hey guys in this post we will discuss the different ways of creating Javascript functions. There are 3 different ways to create javascript functions let’s discuss one by one
Table of Contents
Three different ways
- Function declaration
- Function expression
- Arrow function
Function declaration
Let’s look at the syntax
function (parameters) {
//logic
}
Let’s look at the example
function printMessage(name, age, location) {
console.log(`I am ${name} and i am ${age} year's old. I am living in a beautiful country ${location}`);
}
Function expression
Let’s look at the syntax
const = function(parameters) {
//logic
}
Let’s look at the example
const printMessage = function(name, age, location) {
console.log(`I am ${name} and i am ${age} year's old. I am living in a beautiful country ${location}`);
}
Arrow function
Let’s look at the syntax
const = (parameters) => {
//logic
}
Let’s look at the example
const printMessage = (name, age, location) => {
console.log(`I am ${name} and i am ${age} year's old. I am living in a beautiful country ${location}`);
}
Learn more about arrow function here