Search code examples
javascriptliteralsfunction-literal

Exact meaning of Function literal in JavaScript


In JavaScript there are both Object literals and function literals.

Object literal:

myObject = {myprop:"myValue"}

Function literal:

myFunction = function() {
   alert("hello world");
}

What is the significance of the word literal? Can we say Java has method literals?

public void myMethod() {
    System.out.println("are my literal");
}

Solution

  • A function literal is just an expression that defines an unnamed function.

    The syntax for a function literal is much like that of the function statement, except that it is used as an expression rather than as a statement and no function name is required.

    So When you give the method name then it can't be a method literal.