It's Not Wrong that " ".length == 7

https://news.ycombinator.com/rss Hits: 14
Summary

It’s Not Wrong that "🤦🏼‍♂️".length == 7 But It’s Better that "🤦🏼‍♂️".len() == 17 and Rather Useless that len("🤦🏼‍♂️") == 5 From time to time, someone shows that in JavaScript the .length of a string containing an emoji results in a number greater than 1 (typically 2) and then proceeds to the conclusion that haha JavaScript is so broken—and is rewarded with many likes. In this post, I will try to convince you that ridiculing JavaScript for this is less insightful than it first appears and that Swift’s approach to string length isn’t unambiguously the best one. Python 3’s approach is unambiguously the worst one, though. What’s Going on with the Title? "🤦🏼‍♂️".length == 7 evaluates to true as JavaScript. Let’s try JavaScript console in Firefox: "🤦🏼‍♂️".length == 7 true Haha, right? Well, you’ve been told that the Python community suffered the Python 2 vs. Python 3 split, among other things, to Get Unicode Right. Let’s try Python 3: $ python3 Python 3.6.8 (default, Jan 14 2019, 11:02:34) [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux Type "help", "copyright", "credits" or "license" for more information. >>> len("🤦🏼‍♂️") == 5 True >>> OK, then. Now, Rust has the benefit of learning from languages that came before it. Let’s try Rust: $ cargo new -q length $ cd length $ echo 'fn main() { println!("{}", "🤦🏼‍♂️".len() == 17); }' > src/main.rs $ cargo run -q true That’s better! What? The string contains a single emoji consisting of five Unicode scalar values: Unicode scalarUTF-32 code unitsUTF-16 code unitsUTF-8 code unitsUTF-32 bytesUTF-16 bytesUTF-8 bytes U+1F926 FACE PALM124444 U+1F3FC EMOJI MODIFIER FITZPATRICK TYPE-3124444 U+200D ZERO WIDTH JOINER113423 U+2642 MALE SIGN113423 U+FE0F VARIATION SELECTOR-16113423 Total5717201417 The string that contains one graphical unit consists of 5 Unicode scalar values. First, there’s a base character that means a person face palming. By default, the person would have a cartoonish yellow color. The next character ...

First seen: 2025-08-22 08:02

Last seen: 2025-08-23 07:33