The method that captures the output of console.log of in JS

  • 2020-05-30 19:25:18
  • OfStack

This example shows how to capture the output of console.log () in JS. Share with you for your reference. The specific analysis is as follows:

We know that console.log () can output information to debugger for developers to see. But what if we want to get the output of console.log () in JS? In fact, it is not difficult to save the original console.log and replace it with another implementation. The code is as follows:


var lastLog;
console.oldLog = console.log;
console.log = function(str) {
  console.oldLog(str);
  lastLog = str;
} 
console.log("Hello, Neo");
document.write(lastLog);

In this case, lastLog is "Hello, Neo".

I hope this article has helped you with your javascript programming.


Related articles: