JavaScript Function toString() method - JavaScript Tutorial

JavaScript Function toString() method

BLUF: This tutorial on JavaScript Function toString() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript Function toString() method

Understanding JavaScript Function toString() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience