JavaScript Function, Definition, Declaration and Alternative methods to define JavaScript Function

1.4K views 3 minutes read
A+A-
Reset

A function is a block of code which is used to perform a specific task and get a desired output according to the requirement of users. In simple words, a function is a block of code which does some calculation and give the result.

Structure of JavaScript Function

Below is simple structure or skeleton of JavaScript function.

function MyFunction (par1, par2, par3,,,,,,)
{	
	Code………………..
	………….
}	

How to define & Declare Function in JavaScript?

In JavaScript Functions can be defined using keyword function. After that we write the name of the function (user-defined) followed by the open ‘(‘ and closed ‘)‘ parenthesis containing parameters inside them and the open ‘{‘ and closed ‘}‘ curly braces containing set of code between them to perform the task and return result to the user. This method is known as Function Declaration.

Other Method – Function Expression

Another method to define a function is creating an object as name of the function and declaring that object as function by adding function () on another side of “=” operator followed by open and closed curly braces containing set of code between them. This method is known as Function Expression.

Example:

Let MyFunction = function(par1, par2, par3,,,,,,)
{	
	Code………………..
	………….
}	

We have shared a article about the real-time use of JavaScript function in Email Validation in ASP.NET using JavaScript Regular Expression.

JavaScript Functions
JavaScript Function

Difference between Function Declaration and Function Expression?

The difference between Function Declaration and Function Expression is that the Function Expression does not support the concept of Hoisting.

If you find this article helpful, please leave feedback as comment or on any of our social media. You can also subscribe us on YouTube.

Related Posts

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Index

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.