Skip to main content

toString() on a function

What Function.prototype.toString() does

The toString() method returns a string representation of the function's source code.

In other words, it converts the function into a string.


Example 1 - a simple function

javascript
function sum(a, b) { return a + b; } console.log(sum.toString());

Output:

javascript
"function sum(a, b) { return a + b; }"

Example 2 - an arrow function

javascript
const multiply = (a, b) => a * b; console.log(multiply.toString());

Output:

javascript
"(a, b) => a * b"

Example 3 - an anonymous function

javascript
console.log(function(x) { return x ** 2; }.toString());

Output:

javascript
"function (x) { return x ** 2; }"

Example 4 - built-in (native) functions

If you call toString() on a built-in JavaScript function (for example, Math.max), you will not see the source code, but you will get a special string:

javascript
console.log(Math.max.toString());

Output:

javascript
"function max() { [native code] }"

This means the function is implemented at the engine level (C++), not in JS.


Example 5 - on classes and methods

javascript
class User { sayHi() { console.log('Hello!'); } } console.log(User.toString());

Output:

javascript
"class User { sayHi() { console.log('Hello!'); } }"

Behavior per the standard (ECMAScript 2019+)

Previously, browsers returned different formats (they could strip whitespace, line breaks, and so on), but starting with ES2019 (ES10):

Function.prototype.toString() returns an exact textual copy of the source code, including whitespace, comments, and formatting.


Why this is needed

UseExample
DebuggingYou can print a function's source code to the console
Inspecting contentFor example, for analysis or serialization
MetaprogrammingGenerating new functions "based on" others
Built-in toolsSome frameworks use toString() to extract arguments or a function's body (for example, Angular before ES6)

Example 6 - use in metaprogramming

javascript
function greet(name) { return `Hello, ${name}`; } const src = greet.toString(); console.log(src); // source code const copy = new Function('return ' + src)(); console.log(copy('Tim')); // Hello, Tim

Here we serialized the function into a string, then created a new one from it via new Function.


Important to remember

  1. toString() returns the source code, not a "compiled" variant.
  2. For built-in functions, "[native code]" is returned.
  3. For class methods and generators, the format matches the declaration.
  4. Arrow functions and methods with => are returned as is, without function.

Summary

What it doesReturns the function's source code as a string
Return typestring
Works withRegular functions, arrow functions, methods, classes
For built-in functionsfunction ... { [native code] }
StandardSince ES2019 returns the exact source text
Common usesDebugging, code analysis, metaprogramming

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.