How Many Different ways of Writing Functions in JavaScript

Reading Time: 5 minutes
602 Views

Inside this article we will see the concept i.e How Many Different ways of Writing Functions in JavaScript. Article contains the classified information i.e What are different ways of defining functions in JavaScript.

In JavaScript, a function is a block of reusable code that performs a specific task. Functions are a fundamental building block of JavaScript programming, and they play an essential role in modularizing code, improving code reusability, and promoting code efficiency.

Read More: How To Prevent Website From Being Loaded in Iframe

Let’s get started.

What is a function in Javascript?

A function is a block of code that can be defined and called to perform a specific task. Functions can take input parameters, perform operations on those inputs, and return a result.

They can also be used to modularize code, making it easier to organize and reuse. In short, functions in JavaScript are reusable blocks of code that can perform a specific task and be called multiple times throughout a program.

To define a function in JavaScript, you can use the function keyword followed by the name of the function, a set of parentheses that may contain input parameters, and a block of code enclosed in curly braces that defines what the function does.

function fnName(arg1, arg2, ...) {

  return statement;
}

How ways of writing a function in JavaScript?

Yes, there are three ways of writing a function in JavaScript:

Function Declaration

This is the most common way of defining a function. In this method, you use the function keyword followed by the name of the function and the block of code that defines what the function does.

Here’s an example:

<script>

// Function declaration
function add(a, b) {		
	console.log(a + b);
}

// Calling a function
add(2, 3);

</script>

Function Expressions

Function expressions are similar to function declarations, but they are defined as a variable assignment. Instead of using the function keyword, you can define a function expression by assigning a function to a variable.

Read More: How to Create a Password Strength Validator in PHP Tutorial

Here is an example of a function expression:

<script>
  
	// Function Expression
	const add = function(a, b) {
		console.log(a+b);
	}
	
	// Calling function
	add(2, 3);

</script>

Arrow Functions

Arrow functions are a concise way to define functions in JavaScript. They use the => operator instead of the function keyword, and they don’t require curly braces if the function body consists of a single expression.

Here is an example of an arrow function:

<script>
	// Single line of code
	let add = (a, b) => a + b;
	
	console.log(add(3, 2));
</script>

Note: When there is a need to include multiple lines of code we use brackets. Also, when there are multiple lines of code in the bracket we should write return explicitly to return the value from the function.

Read More: Step by Step To Build Your Own Chrome Extension Tutorial

This is an example with multiple lines of code in arrow function

<script>
	// Multiple line of code
	const great = (a, b) => {
		if (a > b)
			return "a is greater";
		else
			return "b is greater";
	}
	
	console.log(great(3,5));
</script>

We hope this article helped you to learn How Many Different ways of Writing Functions in JavaScript in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more