Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
The following string returns a length of 20 in Javascript, but why?
8080!\u001b[22m\u001b[32m\u001b[39m
\u001b is the Unicode value for Escape which is counted as a single character. With that in mind, the length is 20.
\u001b
Escape
Add a comment
Your strings contains unicode escaped characters, this is the string character by character (using split):
split
var str = '8080!\u001b[22m\u001b[32m\u001b[39m'; console.log(str.split(''));
When JavaScript interprets that string expression, the actual value is: (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);
Required, but never shown
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.
Explore related questions
See similar questions with these tags.