How to console.log a JavaScript Function?

In JavaScript (or Node.js), you can simply use the Object.prototype.toString() method to console.log() (or output) the function itself. For example:

function foo() {
    return 'bar';
}

console.log(foo.toString());

Please note that for built-in methods, you would see [native code] (or something similar) as the output. This is because these methods are not written in JavaScript.


This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.