JavaScript Function toString() method

The toString method in JavaScript is used to obtain a string representation of a function, where the string contains the function's source code.

Syntax

Example

function.toString()

Return Value

It returns a string.

JavaScript Function toString method Example

Example 1

Let's examine an illustration demonstrating the presentation of a function as a string.

Example

<script>

function add(a,b) {

  return a + b;

}

document.writeln(add.toString());

document.writeln(typeof add.toString());

</script>

Output:

Output

"function add(a,b) { return a + b; }" "string"

Example 2

An illustration is provided below demonstrating how to showcase the summation of numbers presented as strings.

Example

<script>

function add(a,b) {

  return a + b;

}	

document.writeln(add(10,20).toString());//30

document.writeln(typeof add(10,20).toString());//string

</script>

Output:

Output

30 string

Example 3

Let's consider an example where we demonstrate how to present the ceiling value of specified numbers as a string.

Example

<script>

function absolute(num) {

  return Math.ceil(num);

}

document.writeln(absolute(15.4).toString());

document.writeln(typeof absolute(15.4).toString());</script>

Output:

Output

16 string

Input Required

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