As of this another question on the site and its answers, I had a question about how to access the characters of a chain.
On the one hand it is suggested to use charAt
(eg cadena.charAt(0)
) to access the character that is in the indicated position, which is correct ... but I have always used the bracket notation (pe cadena[0]
) to access the character of a specific position.
Doing some online research, I have found that bracket access is not supported by older versions of IE (versions that are no longer officially supported, so it would not be a problem), and doing performance tests it seems that access with bracket gets similar (or better) results than charAt
for an example such as:
let valor = 100333;
let valorString = valor.toString().charAt(0);
let valorCorchete = (valor.toString())[0];
console.log(valorString);
console.log(valorCorchete);
So, is there any difference between reading a character using charAt
or using brackets? Are there cases in which it would be more advisable to use one over the other?