JavaScript Functions: A Beginner's Guide to Getting Started with the Basics

JavaScript Functions: A Beginner's Guide to Getting Started with the Basics

Function Declarations

Function declarations are the most common type of function in JavaScript. They are defined using the "function" keyword, followed by the function name and a set of parentheses that may contain parameters.

For example:

function greet(name) {
console.log("Hello, " + name);
}        

Function declarations are hoisted, which means that they can be called from anywhere in the code, even before they are defined.

Function Expressions

Function expressions are similar to function declarations, but they are defined using a variable assignment.

For example:

const greet = function(name) {
console.log("Hello, " + name);
}        

Unlike function declarations, function expressions are not hoisted, so they must be defined before they are called.

Arrow Functions

Arrow functions are a shorthand syntax for defining functions. They are defined using the "=>" operator, which separates the function's parameters from its body.

For example:

const greet = (name) => {
console.log("Hello, " + name);
}        

Arrow functions are often used when defining small, simple functions, and they do not have their own "this" value, which can make them easier to work within certain situations.

Generator Functions

Generator functions are special functions that can be paused and resumed multiple times. They are defined using the "function*" syntax and use the "yield" keyword to pause and return a value. These functions return an iterator object to keep track of where to resume the function.

For example:

function* numbers() {
yield 1;
yield 2;
yield 3;
}        

Generator functions can be useful for creating iterators or for performing complex calculations that can be broken down into smaller steps.

To know more about generator functions : you click here.

Async Functions

Async functions are special functions that are designed to work with asynchronous code, such as code that involves waiting for a network request to complete. They are defined using the "async" keyword and use the "await" keyword to pause execution until a promise is resolved.

For example:

async function getData() {
const response = await fetch("https://example.com/data");
const data = await response.json();
console.log(data);
}        

Async functions are a powerful tool for working with asynchronous code, and they can make it easier to write code that is easier to read and understand.

Anonymous Function

In JavaScript, an anonymous function is a function without a name. It can be stored in a variable or passed as an argument to another function. Anonymous functions are commonly used in JavaScript because they can be defined and used immediately, without the need to give them a name. This can make code simpler and more concise.

Here is an example of an anonymous function being stored in a variable and then invoked:

let greet = function() {
 console.log(“Hello, World! Please read and clap for this article”);
}
greet();         

Anonymous functions can also be used as arguments to other functions.

For example:

function callFunction(fn) {
  fn();
}
callFunction(function() {
  console.log("I am an anonymous function being passed as an argument.");
});        

Constructor Function

In JavaScript, a constructor function is used to create objects. For example,In a constructor function this keyword does not have a value. It is a substitute for the new object. The value of this will become the new object when a new object is created.

function Person(first, last, age) {
 this.firstName = first;
 this.lastName = last;
 this.age = age;
}

const person = new Person('Khushi','Bhambri',20);        

In summary, there are several different types of functions in JavaScript, each with its own unique characteristics and uses. Understanding how to use each type of function can help you write more efficient and effective code.

To view or add a comment, sign in

More articles by Ali Hamza

Explore content categories