Fri Mar 10 2023

Understanding Various Syntax of Functions in JavaScript

JavaScript101 views

JavaScript functions are one of the most powerful and flexible features of the language, enabling developers to write reusable, modular, and clean code. A function is essentially a block of code designed to perform a particular task, and it can be executed whenever it is invoked or called. In this article, we’ll explore various syntaxes for writing functions in JavaScript.

1. Traditional Function

A function declaration is the most common and traditional way of defining a function in JavaScript. It involves using the function keyword followed by a name for the function, a set of parentheses to define parameters (if any), and a block of code.

function functionName(parameters) {
// code to be executed
}

2. Arrow Functions (ES6)

Arrow function was introduced in ECMAScript 6 (ES6). It provides a more concise syntax for writing functions. They are particularly useful for writing shorter functions, especially for callbacks and event handlers. Arrow functions also handle this keyword differently, as they inherit it from their surrounding scope (lexical scoping).

const functionName = (parameters) => {
// code to be executed
};

3. Function Expression

A function expression involves assigning a function to a variable. Unlike function declarations, function expressions are not hoisted, which means you cannot call the function before it’s defined in the code.

const functionName = function(parameters) {
// code to be executed
};

4. Anonymous Functions

An anonymous function is a function without a name. It is usually used in situations where the function does not need to be called elsewhere, such as in event handlers, callbacks, or immediately invoked function expressions (IIFE).

(function() {
// code to be executed
})();

5. Async Functions (ES8)

Async functions were introduced in ES8 (ECMAScript 2017) and allow you to write asynchronous code in a synchronous manner. These functions return a Promise, and they are typically paired with await to handle asynchronous operations.

async function functionName() {
let result = await someAsyncTask();
return result;
}

Conclusion

JavaScript provides a wide variety of syntaxes for defining functions, each suited for different use cases. Whether you need a simple function declaration for reusable code, an anonymous function for quick callbacks, or an async function for handling asynchronous tasks, understanding these different syntaxes will make you a more versatile JavaScript developer.

Understanding Various Syntax of Functions in JavaScript

File Name: javascript-function-syntax.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Function Syntax</title>
    <script>
        /* Tradition Function */
        function sum1(x, y) {
            return x+y;
        }

        /* Anonymous Function Assigned to a Variable */ 
        const sum2 = function (x, y) {
            return x + y;
        }

        /* Arrow Function */
        const sum3 = (x, y) => {
            return x + y;
        }

        function calculator() {
            document.getElementById('fun1').innerHTML = sum1(5, 6);
            document.getElementById('fun2').innerHTML = sum2(6, 7);
            document.getElementById('fun3').innerHTML = sum3(7, 8);
        }
        
    </script>
</head>
<body>
    <button onclick="calculator()">Calculate</button>
    <h1>Traditional Function: <span id="fun1"></span></h1>
    <h1>Anonymous Function: <span id="fun2"></span></h1>
    <h1>Arrow Function: <span id="fun3"></span></h1>
</body>
</html>

Result Screenshot(s)

Understanding Various Syntax of Functions in JavaScriptWorking Sample0

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.