Types of Functions in JavaScript (English)

This article introduces various types of functions in JavaScript, helpful for beginner developers. Functions are crucial concepts in JavaScript programming, and understanding these different types of functions enables writing efficient code.

Methods

  1. Named Functions: Functions declared with a specific name that users can refer to, also known as declarative functions.

  2. Anonymous Functions: Functions that can be declared without specifying a name, created using variables or call expressions.

  3. Arrow Functions: Functions with a concise syntax for creating them.

  4. Immediately Invoked Function Expressions: Functions that execute immediately after they are defined, preventing hoisting in local blocks and maintaining privacy by protecting global variables from pollution.

  5. Higher Order Functions: Functions that take one or more functions as arguments and return a function as a result.

  6. Constructor Function: Special functions used to create objects.


Named Functions

Named functions are the most traditional way of defining and calling functions, using a specific name. You can call these functions anytime within your code using their function names, enhancing code readability and reusability.

Named Functions

function greetPlanet(){
    console.log("Hello, Planet");
}
greetPlanet();

Anonymous Functions

Anonymous functions, also known as nameless functions, are primarily used as a way to assign functions to variables. These functions can be passed as arguments to other functions and are created using function expressions.

Anonymous Functions

let greet = function (name) {
    console.log(`안녕, ${name}`);
}

greet("KJ");

Arrow Functions

Arrow functions are a function definition introduced in ES6, mainly used to express short functions concisely. They are especially convenient when the function body consists of only one line.

Arrow Functions

let greet = () => console.log("안녕");
greet();

Immediately Invoked Function Expressions

Immediately Invoked Function Expressions (IIFE) are functions that are executed immediately after they are defined. They are called as soon as they are defined. This prevents pollution of the global scope and allows the creation of private scopes

Immediately Invoked Function Expressions

(function () {
    let str = "Hello World!";
    console.log(str);
})();

Higher Order Functions

Higher-order functions refer to functions that either take other functions as arguments or return functions. These are important concepts in functional programming and are primarily used in array manipulation functions such as map, filter, and reduce

Higher Order Functions

let arr = [20, 30, 40];

let newArr = arr.map((el) => el + 10);
console.log(newArr);

Constructor Function

A constructor function serves as a blueprint for creating multiple objects with similar properties and methods. It is used to design objects with similar features. The new keyword is used to create new object instances based on this blueprint.

Constructor Function

function Person(name, place) {
    this.name = name;
    this.place = place;
}

let user1 = new Person("SJ", "Korea");

console.log(`Hello, World! ${user1.name} from ${user1.place}.`);