JavaScript Function toString()
Description
The toString() method returns the source code of a function as a string.
The toString() method does not change the original function.
Example
function myFunction() {
return "Hello";
}
let text = myFunction.toString();
Try it Yourself »
Same as:
let text = myFunction;
Try it Yourself »Note
Every JavaScript object has a toString() method.
The toString() method is used by JavaScript
when an object needs to be displayed as a text (like in HTML),
or when an object needs to be used as a string.
Syntax
function.toString()
Parameters
| NONE |
Return Value
| Type | Description |
| String | The source code of the function as a string. |
Function Methods & Properties
Browser Support
function.toString() is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera |