Reputation: 494
The following string returns a length of 20 in Javascript, but why?
8080!\u001b[22m\u001b[32m\u001b[39m
Upvotes: 2
Views: 281
Reputation: 14958
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);
Upvotes: 2
Reputation: 22776
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(''));
Upvotes: 2
Reputation: 9172
\u001b
is the Unicode value for Escape
which is counted as a single character. With that in mind, the length is 20.
Upvotes: 5