Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.charAt() vs String.at()

What is the difference between the "String.charAt()" method and the "String.at()" method?

I tried to understand how they work, but other than the fact that the "at" method can have a negative value, I did not find any more.

like image 647
Player_50B Avatar asked May 10 '26 08:05

Player_50B


1 Answers

at() is a newer addition to JavaScript compared to charAt(). According to MDN, both charAt() and at() are valid, but at() is more "succinct and readable". The ability to use negative indexes makes your code more concise since you can do things like myString.at(-2) instead of myString.charAt(myString.length - 2).

charAt() has better support of older browser versions (1, 2), but I would give that absolutely no consideration, unless you have an extremely specific use case.

like image 73
Pavel Savva Avatar answered May 12 '26 23:05

Pavel Savva