JS Property String.length : How do you count special characters?


#1

Hi there!

In JavaScript, how do you count the length of a string if it has special characters?
My native language is spanish. We frecuently use characters like ñ, á, é, í, ó, ú, ü


#2

The String.length property works on any unicode string, and counts all characters in the string without regard of special or not.

 > ['ñ', 'á', 'é', 'í', 'ó', 'ú', 'ü'].length
<- 7
 > ['ñ', 'á', 'é', 'í', 'ó', 'ú', 'ü'].join('').length
<- 7

As we see, both Array.length and String.length work as expected.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.