Suggest an editImprove this articleRefine the answer for “toString() on a function”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`Function.prototype.toString()`** returns a string representation of the function's source code - in other words, it converts the function into a string. **Key point:** starting with ES2019, the method returns an exact textual copy of the source code, including whitespace, comments, and formatting, and for built-in functions it returns `"[native code]"`.Shown above the full answer for quick recall.Answer (EN)Image## 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 | Use | Example | |---|---| | Debugging | You can print a function's source code to the console | | Inspecting content | For example, for analysis or serialization | | Metaprogramming | Generating new functions "based on" others | | Built-in tools | Some 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 does | Returns the function's source code as a string | |---|---| | Return type | `string` | | Works with | Regular functions, arrow functions, methods, classes | | For built-in functions | `function ... { [native code] }` | | Standard | Since ES2019 returns the exact source text | | Common uses | Debugging, code analysis, metaprogramming |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.