0

While reading charAt in MDN, I've found that strings in javascript sequence of UTF-16?

So to verify my understanding, I've tried the following script in NodeJS

Buffer.from("€").length // gives 3

How can this be 3 bytes? As far as I know, this should be either 2 or 4 bytes, How can UTF-16 string give 3 bytes?

I've looked at different questions here in StackOverflow but didn't find an explanation for this part.

1 Answer 1

1

Because when creating a Buffer.from a string, it needs an encoding, and if you don't specify one, it defaults to UTF-8.

You seem to be looking for either

Buffer.from("€", "utf16le").length

or just

"€".length * 2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.