JavaScript Tutorials

Function Expressions

This is an important concept that we’ll need later on—functions in JavaScript are actually data. This means that the following two ways to define a function are exactly the same:

function f(){return 1;}
var f = function(){return 1;}

The second way of defining a function is known as function literal notation. When you use the typeof operator on a variable that holds a function value, it returns the string “function”.

function f(){return 1;}
typeof f

So JavaScript functions are data, but a special kind of data with two important features:

  • They contain code
  • They are executable (can be invoked)
var sum = function(a, b) {return a + b;}
var add = sum;
delete sum

typeof sum;

typeof add;

add(1, 2);

As you saw before, the way to execute a function is by adding parentheses after its name. As the next example demonstrates, this works regardless of how the function was defined. In the example, you can also see how a function is treated as a normal variable—it can be copied to a different variable and even deleted.

Because functions are data assigned to variables, the same rules for naming functions apply as for naming variables—a function name cannot start with a number and it can contain any combination of letters, numbers, and the underscore character.


1. Choose the correct code to be inserted in order to invoke the function.

var fun=function lfc(x){



A. thisfun(10)

B. lfc(10)

C. function thisfun(10)

D. None of the above

2. How many arguments does function foo accept?

const foo = function(a, b){

    return a+b;


A. 0

B. 1

C. 2

D. 3

3. What will be the output of the below code?

var changeVal = function(p){

    return p+2;



A. 4

B. 22

C. Error: changeVal is not a function

D. undefined

4. What is the output of this program?


   var f91 = function () {



A. f91

B. Error

C. undefined

D. None of these