JavaScript Function Literals

A Function Literal is an expression that defines an unnamed function. So why it is called Functional Literals?

Because it has an unnamed function within a variable just along with its parameters. So, we call this unnamed function the function literal.

Syntax

It is much alike to how a normal function statement is written but instead of a function name, it is confined within a variable that is defined.

var varName = function(parameters)
{
    //body of the function
}

Example

var number = function(x, y)
{
    return x + y;
}

var sum = number(50, 50)
console.log(sum)

Output: 100

So as we can see here, I have created a function literal passing the parameters as x and y but it has no name like a normal function, that's the goal of it. So we can simply then access the function with the expression we declared called the number and pass in the arguments for the parameters.

Wed, 10/27/2021 - 12:44

Authored by

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.
Tags