2

The following string returns a length of 20 in Javascript, but why?

8080!\u001b[22m\u001b[32m\u001b[39m

2

3 Answers 3

5

\u001b is the Unicode value for Escape which is counted as a single character. With that in mind, the length is 20.

Sign up to request clarification or add additional context in comments.

Comments

2

Your strings contains unicode escaped characters, this is the string character by character (using split):

var str = '8080!\u001b[22m\u001b[32m\u001b[39m';

console.log(str.split(''));

Comments

2

When JavaScript interprets that string expression, the actual value is: Image of string evaluated by JavaScript (length 20).

See it below:

console.log('8080!\u001b[22m\u001b[32m\u001b[39m');

console.log('length:', '8080!\u001b[22m\u001b[32m\u001b[39m'.length);

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.