Function.prototype.toSource()

Obsolete
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

The toSource() method returns a string representing the source code of the object. This method is usually called internally by JavaScript and not explicitly in code. You can call toSource() while debugging to examine the contents of an object.

Syntax

function.toSource();

Return value

A string representing the source code of the object.

Examples

Native functions

For the built-in Function object, toSource() returns the following string indicating that the source code is not available:

function Function() {
  [native code]
}

Custom functions

For custom functions, toSource() returns the JavaScript source that defines the object as a string.

// For example:
function hello() {
  console.log("Hello, World!");
}

hello.toSource();
// Results in:
"function hello() { 
    console.log(\"Hello, World!\");
}"

Specifications

Not part of any standard.

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
toSource
Non-standard
Chrome No support NoEdge No support NoFirefox No support 1 — 74
Notes
No support 1 — 74
Notes
Notes Starting in Firefox 74, toSource() is no longer available for use by web content. It is still allowed for internal and privileged code.
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 4Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No

Legend

Full support  
Full support
No support  
No support
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
See implementation notes.
See implementation notes.

See also