The toString()
method returns a string representing the source code of the function.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
function.toString()
Return value
A string representing the source code of the function.
Description
The Function
object overrides the toString
method inherited from Object
; it does not inherit Object.prototype.toString
. For user-defined Function
objects, the toString
method returns a string containing the source text segment which was used to define the function.
JavaScript calls the toString
method automatically when a Function
is to be represented as a text value, e.g. when a function is concatenated with a string.
The toString()
method will throw a TypeError
exception ("Function.prototype.toString called on incompatible object"), if its this
value object is not a Function
object.
Function.prototype.toString.call('foo'); // TypeError
If the toString()
method is called on built-in function objects or a function created by Function.prototype.bind
, toString()
returns a native function string which looks like
"function () {\n [native code]\n}"
If the toString()
method is called on a function created by the Function
constructor, toString()
returns the source code of a synthesized function declaration named "anonymous" using the provided parameters and function body.
Examples
Function | Function.prototype.toString result |
---|---|
function f(){} |
"function f(){}" |
class A { a(){} } |
"class A { a(){} }" |
function* g(){} |
"function* g(){}" |
a => a |
"a => a" |
({ a(){} }.a) |
"a(){}" |
({ *a(){} }.a) |
"*a(){}" |
({ [0](){} }[0]) |
"[0](){}" |
Object.getOwnPropertyDescriptor({ get a(){} }, "a").get |
"get a(){}" |
Object.getOwnPropertyDescriptor({ set a(x){} }, "a").set |
"set a(x){}" |
Function.prototype.toString |
"function toString() { [native code] }" |
(function f(){}.bind(0)) |
"function () { [native code] }" |
Function("a", "b") |
"function anonymous(a\n) {\nb\n}" |
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.1. |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Function.prototype.toString' in that specification. |
Standard | Added more specific requirements for the string representation. |
Function.prototype.toString revisions proposal |
Draft | Standardizes native function string, line endings. |
ECMAScript Latest Draft (ECMA-262) The definition of 'Function.prototype.toString' in that specification. |
Draft |
Browser compatibility
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toString | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 5 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Support of toString revision | Chrome No support No | Edge No support No | Firefox Full support 54 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android Full support 54 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
Legend
- Full support
- Full support
- No support
- No support
Firefox-specific notes
- Since Firefox 17,
Function.prototype.toString()
has been implemented by saving the function's source. The decompiler was removed, so that theindentation
parameter is not needed any more. See bug 761723 for more details. - From with Firefox 38 to 63,
Function.prototype.toString()
was throwing forProxy
objects (bug 1100936 and bug 1440468).