'How `String["fromCharCode"]` acts like `String.fromCharCode()` in javascript? [duplicate]

Search engine will ignore the [ ] ", so I can't get the answer. I took a test and find out they resulted the same, but I'm a js rookie and I don't know how they equals to each other.



Solution 1:[1]

fromCharCode 'lives' as a static method of the String Object. Such a method has a key and a value, visualised: {fromCharCode: function() {...}}. You can retrieve (access) the method using it's key directly String.fromCharCode or 'bracket' style String['fromCharCode']. This is true for all Objects.

The bracket notation may be usefull when the key contains special characters, see snippet.

See also

const someObj = {
  dotNotation: 'hello world',
  'non dot Notation': 'hello world you too',
};

console.log(someObj.dotNotation);
// the last property can not be retrieved with dot notation
// console.log(someObj.non dot Notation);
// so
console.log(someObj['non dot Notation']);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1