We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

String Encoding

In JavaScript, strings consist of UTF-16 code units - which basically means that most characters are represented by a 16-bit number (2 bytes). This allows for more than just the 128 ASCII characters, but also characters from non-English languages and other symbols.

Some characters (like emojis 😀) require more than 16 bits to represent, so JavaScript uses a pair of 16-bit numbers (2 code units) to represent them.

So... Who Cares?

Long story short, you're generally safe to use unicode characters (like emojis) in your strings. Just be aware that some characters will take up more than one "character" in the string. This is totally chill:

const kermit = "🐸";

Assignment

  1. Run the given code.
  2. Notice the number of code units and perceived characters in the string "boots".
  3. Replace the content of the name variable with the bear emoji ('🐻') and run the code again.
  4. Notice the difference between lengths and counts compared to the simple string!
  5. Submit the code with the bear emoji.