Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Length of string in JavaScript

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

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

like image 458
Kossi D. T. S. Avatar asked May 30 '26 19:05

Kossi D. T. S.


2 Answers

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

like image 53
Richie Bendall Avatar answered Jun 02 '26 08:06

Richie Bendall


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(''));
like image 43
MrGeek Avatar answered Jun 02 '26 08:06

MrGeek