The toString() method returns a string representing the source code of the function.



Return value

A string representing the source code of the function.


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.'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.


Comparing actual source code and toString results

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)
({ [0](){} }[0])
    get a(){}
}, "a").get
"get a(){}"
    set a(x){}
}, "a").set
"set a(x){}"
"function toString() { [native code] }"
(function f(){}.bind(0))
"function () { [native code] }"
Function("a", "b")
"function anonymous(a\n) {\nb\n}"


ECMAScript (ECMA-262)
The definition of 'Function.prototype.toString' in that specification.

Browser compatibility

ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
toStringChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support Yes
Support of toString revisionChrome No support NoEdge No support NoFirefox Full support 54IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 54Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No


Full support
Full support
No support
No support

See also