String.prototype behavior

String.prototype.hello = function() {
  console.log(`Hello ${this}`);

let name = new String('Carl');

can anyone point out for me why name.hello(); works but 'Carl'.hello(); does not?

I just copy/pasted your code and ran it with node and both work as intended. What makes you think the second one isn’t working?


Do not use let because you already declare name with ‘new String(…)’.
That means you have to choose between:

  1. let name = ‘Carl’;
  2. name = new String(‘Carl’); //without let!