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

Try it



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'); // throws TypeError

If the toString() method is called on built-in function objects, a function created by Function.prototype.bind(), or other non-JavaScript functions, then toString() returns a native 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, or 1.

Note: This means using eval() on native function strings is a guaranteed syntax error.

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. For example, Function("a", "b", "return a + b").toString() will return:

"function anonymous(a,b\n) {\nreturn a + b\n}"

Since ES2018, the spec requires the return value of toString() 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 the compatibility table.


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}"

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 is exact, 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' }"


ECMAScript Language Specification
# sec-function.prototype.tostring

Browser compatibility

BCD tables only load in the browser

See also