Depending on your definition of “character”, all answers posted so far are incorrect. The string.length
answer is only reliable when you’re certain that only BMP Unicode symbols will be entered. For example, 'a'.length == 1
, as you’d expect.
However, for supplementary (non-BMP) symbols, things are a bit different. For example, ''.length == 2
, even though there’s only one Unicode symbol there. This is because JavaScript exposes UCS-2 code units as “characters”.
Luckily, it’s still possible to count the number of Unicode symbols in a JavaScript string through some hackery. You could use Punycode.js’s utility functions to convert between UCS-2 strings and UTF-16 code points for this:
// `String.length` replacement that only counts full Unicode characters
punycode.ucs2.decode('a').length; // 1
punycode.ucs2.decode('').length; // 1 (note that `''.length == 2`!)