Different ways to create a function in javascript
In this chapter, you will learn about different ways to create a function in javascript. This chapter helps you to understand how many ways we can create a function in Javascript. We can create the function in Javascript in different ways. Let’s look at the below examples
Different ways to create a function in Javascript:
Function declaration:
The function declaration approach created a regular function, the function starts with the keyword function followed by function name.
// function declaration
function fullNameByDec(firstName, lastName) {
return firstName + ' '+ lastName;
}
console.log(fullNameByDec('John','Mickel'));
// output
// John Mickel
// function declaration
function fullNameByDec(firstName, lastName) {
return firstName + ' '+ lastName;
}
console.log(fullNameByDec('John','Mickel'));
// output
// John Mickel
// function declaration function fullNameByDec(firstName, lastName) { return firstName + ' '+ lastName; } console.log(fullNameByDec('John','Mickel')); // output // John Mickel
Function expression:
// function expression
const fullNameByExp = function(firstName, lastName) {
return firstName + ' '+ lastName;
}
console.log(fullNameByExp('John','Mickel'));
// output
// John Mickel
// function expression
const fullNameByExp = function(firstName, lastName) {
return firstName + ' '+ lastName;
}
console.log(fullNameByExp('John','Mickel'));
// output
// John Mickel
// function expression const fullNameByExp = function(firstName, lastName) { return firstName + ' '+ lastName; } console.log(fullNameByExp('John','Mickel')); // output // John Mickel
Arrow function:
// function
const fullNameByArrow = (firstName, lastName) => {
return firstName + ' '+ lastName;
}
console.log(fullNameByArrow('John','Mickel'));
// output
// John Mickel
// function
const fullNameByArrow = (firstName, lastName) => {
return firstName + ' '+ lastName;
}
console.log(fullNameByArrow('John','Mickel'));
// output
// John Mickel
// function const fullNameByArrow = (firstName, lastName) => { return firstName + ' '+ lastName; } console.log(fullNameByArrow('John','Mickel')); // output // John Mickel
One thought on “Different ways to create a function in javascript”
Comments are closed.