JavaScript Function Literals

Profile picture for user arilio666

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.