Skip to main content

What is function in JavaScript and how to declare it?

function image

In JavaScript, a function is a block of reusable code designed to perform a particular task. Functions provide a way to package code into reusable modules, making code more organized, easier to understand, and promoting code reusability.

There are several ways to declare functions in modern JavaScript. Here are some examples:

Function Declaration:

In this example, greet is the name of the function, and name is a parameter. This function can be called later by using greet("John"), for example.

Function Expression:

In this example, greet is a variable that holds an anonymous function. This function can also be called with greet("John").

Arrow Function:

This is a more concise way to define functions introduced in ES6. It behaves similarly to function expressions but has a shorter syntax.

Method Definition in an Object:

In this example, greet is a method of the person object.

Function Constructor (not recommended):

This is a less common way to define functions and is generally not recommended due to security risks and performance concerns.

Functions can also have default parameters, rest parameters, and can return values. Here's an example of a function with default parameter and return value:

In modern JavaScript, const and let are generally used to declare functions as it helps in avoiding accidental reassignments. Arrow functions are preferred for their concise syntax, especially for simple functions or when working with callbacks.

If you like it or want to add or change something kindly share your thoughts. Thank You.

Comments

Popular Posts

OnePlus Unveils Revolutionary Mechanical Keyboard - A Game-Changer in the Tech World!

A good and surprisingly unique first-time mechanical keyboard, courtesy of Keychron’s impeccable design

OnePlus Monitor X27: A Stunning Display of Performance and Versatility

The OnePlus Monitor X27 is a 27-inch monitor aimed primarily at those who need to push the envelope when it comes to gaming, and maybe their professional life.

What is class in JavaScript ? How to define and use it?

In JavaScript, a class is a blueprint for creating objects with similar properties and methods. It provides a way to define a particular type of object, encapsulating data for the object and methods to operate on that data. Classes were introduced in ECMAScript 2015 (ES6) to mimic the class-based inheritance found in other object-oriented programming languages.  Here's how you define a class in JavaScript:       In this example:  MyClass is the name of the class. Constructor is a special method for creating and initializing objects created with a class. It is executed automatically when a new object is created with the new keyword.  Method1 and method2 are methods of the class, defining behaviour for instances of the class. To create an instance of a class, you use the new keyword: This creates a new object obj based on the MyClass blueprint, with property1 set to 'value1' and property2 set to 'value2'.   You can then access properties and methods of the o