JavaScript Function toString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Instance method

Definition

func.toString() returns the code of a function as text, written exactly as it appears in your program, including spaces and comments. For a function you wrote, you get your own code back. For a built-in function such as Math.max, you get a short text that says [native code], because the real code is hidden inside the browser.

JavaScript calls it for you whenever a function has to become text, for example in String(func), in "" + func, or in a template literal. It is useful for debugging, for quick checks, and for teaching tools that show the code of a function.

Remember
func.toString()

your own function → its code, as written
built-in function → "function name() { [native code] }"
class             → the whole class code
returns           → a string

The exact text of built-in functions
can differ between browsers.

Syntax

Call toString() on any function. It takes no arguments. You can also let JavaScript call it for you with String() or +.

JavaScript
func.toString();
String(func);
CallWhat it does
add.toString()Returns the code of add as text, exactly as you wrote it.
String(add)Same result. JavaScript calls toString() for you. "" + add and template literals do the same.
Math.max.toString()A built-in function. The text contains [native code] instead of the real code.

Built into JavaScript (ES1). Every function has it; no import required. Siblings: apply(), bind() and call().

Library declarations
JavaScript
// Function.prototype.toString (ES1)
// exact source text is required since ES2019
func.toString()

// returns string

Parameters

ParameterDescription
(none)No parameters. The method reads the function it is called on.

Return Value

Return ValueDescription
Your own functionThe code of the function as text, from the first character to the last, exactly as written.
A built-in functionText like "function max() { [native code] }". The exact wording can differ between engines.
TypeError (thrown)The method was called on something that is not a function, for example Function.prototype.toString.call({}).

JavaScript Examples

Example 1 — Turn Functions into Text

A normal function and an arrow function both give back their code. String() and + call toString() for you.

JavaScript
function add(a, b) { return a + b; }
const double = (n) => n * 2;

console.log(add.toString());
console.log(double.toString());

console.log(String(add) === add.toString());
console.log("Text: " + double);
Try It Yourself

How It Works

  1. Your own code comes back. add.toString() returns the function exactly as it was written. The arrow function returns only the arrow part, (n) => n * 2, not the const double = part.
  2. String() does the same. String(add) calls toString() for you, so the comparison is true.
  3. The plus sign too. In "Text: " + double, JavaScript turns the function into text before joining it.

Example 2 — Look at Built-in Functions and Classes

Built-in functions hide their code. A class returns all of its own code, line by line.

JavaScript
console.log(Math.max.toString().includes("[native code]"));
console.log([].push.toString().includes("[native code]"));

class Point {
  constructor(x) {
    this.x = x;
  }
}

const text = Point.toString();

console.log(text.startsWith("class Point"));
console.log("Lines in the class text: " + text.split("\n").length);
Try It Yourself

How It Works

  1. Hidden code. Math.max and push are built into the engine, so their text contains [native code] instead of real code. The exact wording around it can differ between browsers, so the example only searches for that marker.
  2. A class is a function. Point.toString() returns the whole class, beginning with class Point.
  3. Count the lines. The class was written on 5 lines, and the text keeps all of them. Splitting at "\n" gives 5 parts.

Example 3 — Give One Function Its Own toString()

A function is an object, so you can give it its own toString(). The original is still available through Function.prototype.

JavaScript
function greet() { return "hi"; }

greet.toString = function () {
  return "[greet function]";
};

console.log("Own toString: " + greet);
console.log("Original: " + Function.prototype.toString.call(greet));

try {
  Function.prototype.toString.call({});
} catch (err) {
  console.log("Not a function: " + err.name);
}
Try It Yourself

How It Works

  1. Replace it. Setting greet.toString puts a new method on that one function. Now "Own toString: " + greet uses your text.
  2. The real code is still there. Function.prototype.toString.call(greet) runs the built-in method on greet and returns its code, ignoring your replacement.
  3. Only functions are allowed. Calling the built-in method on a plain object {} throws a TypeError.

Browser & Runtime Support

toString() on functions has existed since the first version of JavaScript. Every browser and Node.js version has it.

✓ ES1 · Works everywhere

func.toString()

Supported in all browsers and in Node.js, Deno and Bun. Modern engines also return the exact original text, including comments and spaces.

ES1 Standard since
Google Chrome All versions · Desktop & Mobile
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Opera All versions
Full support
Samsung Internet All versions · Android
Full support
Android WebView All versions
Full support
Node.js All versions · Server runtime
Supported
Deno All versions · JavaScript runtime
Supported
Bun All versions · JavaScript runtime
Supported
Internet Explorer All versions
Full support
func.toString() Excellent

Bottom line: Safe to call anywhere. Old engines may change the spacing of the text or skip comments, so do not depend on the exact characters.

Frequently Asked Questions

The code of the function as text, written exactly as in your program, including spaces and comments. For a built-in function it returns text that contains [native code] instead of the real code.
Yes. Current JavaScript requires the text to match the original source exactly, so comments, line breaks and spaces inside the function are kept. Very old engines were allowed to change them.
Functions such as Math.max or Array.prototype.push are built into the browser or Node.js, so their code is not written in JavaScript. The engine shows a placeholder. Bound functions made with bind() also show [native code].
It is not a good idea. If you turn the text back into a function with new Function() or eval(), the new function cannot see the variables the original used, and eval() can be unsafe. Pass the function itself instead.
It runs the built-in toString() on fn, even when fn has its own toString property that replaces it. It throws a TypeError if the value is not a function.

Back to the index

You have seen all four Function methods. Open the index to review them side by side.

Function Methods index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful