0

I was hoping I could override what is displayed by console.log. I was assuming the below:

const arr = ['ben', 'john', 'amy']; 
Array.prototype.toString = function(){return 'alex';}
console.log(arr); // hoping this could result in 'alex' being printed to the screen. Prints the array.  

I was hoping I could make the array print my 'alex' override to the screen but it looks like that is not how console.log works. The toString function must not be was is used to determine the log. Does anyone know how this is determined or if it can be overridden?

trebek1
  • 755
  • 1
  • 8
  • 28
  • 1
    `console.log` doesn't cast its argument to a string, but does some implementation-specific thing. Which `console` implementation are you using? Does your script run in a browser (which?), in nodejs, or somewhere else? – Bergi Oct 20 '19 at 23:44
  • I tried in chrome and node – trebek1 Oct 20 '19 at 23:45
  • It's not possible to change the inspector display of the Chrome debugger. For the rest, see https://stackoverflow.com/questions/24902061/is-there-an-repr-equivalent-for-javascript – Bergi Oct 20 '19 at 23:52

0 Answers0