Function.prototype.toString()
BaselineWidely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
ThetoString()
method ofFunction
instances returns a string representing the source code of this function.
Try it
function sum(a, b) { return a + b;}console.log(sum.toString());// Expected output: "function sum(a, b) {// return a + b;// }"console.log(Math.abs.toString());// Expected output: "function abs() { [native code] }"
Syntax
toString()
Parameters
None.
Return value
A string representing the source code of the function.
Description
TheFunction
object overrides thetoString()
methodinherited fromObject
; it does not inheritObject.prototype.toString
. For user-definedFunction
objects, thetoString
method returns a string containing the source textsegment which was used to define the function.
JavaScript calls thetoString
method automatically when aFunction
is to be represented as a text value, e.g., when a function isconcatenated with a string.
ThetoString()
method will throw aTypeError
exception("Function.prototype.toString called on incompatible object"), if itsthis
value object is not aFunction
object.
Function.prototype.toString.call("foo"); // throws TypeError
If thetoString()
method is called on built-in function objects, afunction created byFunction.prototype.bind()
, orother non-JavaScript functions, thentoString()
returns anative function string which looks like
function someName() { [native code] }
For intrinsic object methods and functions,someName
is the initial name of the function; otherwise its content may be implementation-defined, but will always be in property name syntax, like[1 + 1]
,someName
, or1
.
Note:This means usingeval()
on native function strings is a guaranteed syntax error.
If thetoString()
method is called on a function created by theFunction
constructor,toString()
returns the source code of a synthesized function declaration named "anonymous" using the provided parameters and function body. For example,Function("a", "b", "return a + b").toString()
will return:
function anonymous(a,b) {return a + b}
Since ES2018, the spec requires the return value oftoString()
to be the exact same source code as it was declared, including any whitespace and/or comments — or, if the host doesn't have the source code available for some reason, requires returning a native function string. Support for this revised behavior can be found in thecompatibility table.
Examples
Comparing actual source code and toString results
function test(fn) { console.log(fn.toString());}function f() {}class A { a() {}}function* g() {}test(f); // "function f() {}"test(A); // "class A { a() {} }"test(g); // "function* g() {}"test((a) => a); // "(a) => a"test({ a() {} }.a); // "a() {}"test({ *a() {} }.a); // "*a() {}"test({ [0]() {} }[0]); // "[0]() {}"test(Object.getOwnPropertyDescriptor({ get a() {} }, "a").get); // "get a() {}"test(Object.getOwnPropertyDescriptor({ set a(x) {} }, "a").set); // "set a(x) {}"test(Function.prototype.toString); // "function toString() { [native code] }"test(function f() {}.bind(0)); // "function () { [native code] }"test(Function("a", "b")); // function anonymous(a\n) {\nb\n}
Note that after theFunction.prototype.toString()
revision, whentoString()
is called, implementations are never allowed to synthesize a function's source that is not a native function string. The method always returns the exact source code used to create the function — including thegetter andsetter examples above. TheFunction
constructor itself has the capability of synthesizing the source code for the function (and is therefore a form of impliciteval()
).
Getting source text of a function
It is possible to get the source text of a function by coercing it to a string — for example, by wrapping it in a template literal:
function foo() { return "bar";}console.log(`${foo}`);// function foo() {// return "bar";// }
This source text isexact, including any interspersed comments (which won't be stored by the engine's internal representation otherwise).
function foo /* a comment */() { return "bar";}console.log(foo.toString());// function foo /* a comment */() {// return "bar";// }
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-function.prototype.tostring |