How and why does ‘a'[‘toUpperCase’]() in JavaScript work?

To break it down.

  • .toUpperCase() is a method of String.prototype
  • 'a' is a primitive value, but gets converted into its Object representation
  • We have two possible notations to access object properties/methods, dot and bracket notation

So

'a'['toUpperCase'];

is the access via bracket notation on the property toUpperCase, from String.prototype. Since this property references a method, we can invoke it by attaching ()

'a'['toUpperCase']();

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)