JavaScript Function call() Method

The call method in JavaScript is utilized to invoke a function with a specific 'this' value and individual arguments. In contrast to the apply method, call accepts arguments as a list.

Syntax

Example

function.call(thisArg, arg1,arg2,....,argn)

Parameter

The parameter thisArg is not mandatory. It represents the value of this that will be used when the function is called.

The arguments, denoted as arg1, arg2, up to argn, are optional and are used to pass values to the function.

Return Value

The return statement in programming gives back the outcome of a function call along with the specified value and parameters.

JavaScript Function call method Example

Example 1

Let's see a simple example of call method.

Example

<script>

function Emp(id,name) {

  this.id = id;	

  this.name = name;

}

function PermanentEmp(id,name) {

 Emp.call(this,id,name);

}

document.writeln(new PermanentEmp(101,"John Martin").name);

</script>

Output:

Output

John Martin

Example 2

Let's see an example of call method.

Example

<script>

function Emp(id,name) {

  this.id = id;

  this.name = name;

}

function PermanentEmp(id,name) {

 Emp.call(this,id,name);

}

function TemporaryEmp(id,name) {

 Emp.call(this,id,name);

}

var p_emp=new PermanentEmp(101,"John Martin");

var t_emp=new TemporaryEmp(201,"Duke William")

document.writeln(p_emp.id+" "+p_emp.name+"<br>");

document.writeln(t_emp.id+" "+t_emp.name);</script>

Output:

Output

101 John Martin

201 Duke William

Input Required

This code uses input(). Please provide values below: