JavaScript Function toString()
Description
ThetoString() method returns the source code of a function as a string.
ThetoString() method does not change the original function.
Example
function myFunction() {
return "Hello";
}
let text = myFunction.toString();
Try it Yourself »return "Hello";
}
let text = myFunction.toString();
Same as:
let text = myFunction;
Try it Yourself »Note
Every JavaScript object has atoString() method.
ThetoString() method is used by JavaScriptwhen 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 |

