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);
Call
What 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
Parameter
Description
(none)
No parameters. The method reads the function it is called on.
Return Value
Return Value
Description
Your own function
The code of the function as text, from the first character to the last, exactly as written.
A built-in function
Text 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);
function add(a, b) { return a + b; }
(n) => n * 2
true
Text: (n) => n * 2
How It Works
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.
String() does the same.String(add) calls toString() for you, so the comparison is true.
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);
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.
A class is a function.Point.toString() returns the whole class, beginning with class Point.
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.
Own toString: [greet function]
Original: function greet() { return "hi"; }
Not a function: TypeError
How It Works
Replace it. Setting greet.toString puts a new method on that one function. Now "Own toString: " + greet uses your text.
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.
Only functions are allowed. Calling the built-in method on a plain object {} throws a TypeError.
Compatibility
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.
ES1Standard since
Google ChromeAll versions · Desktop & Mobile
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
OperaAll versions
Full support
Samsung InternetAll versions · Android
Full support
Android WebViewAll versions
Full support
Node.jsAll versions · Server runtime
Supported
DenoAll versions · JavaScript runtime
Supported
BunAll versions · JavaScript runtime
Supported
Internet ExplorerAll 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.