Kossi D. T. S.
Kossi D. T. S.

Reputation: 494

Length of string in JavaScript

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

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

Upvotes: 2

Views: 281

Answers (3)

lealceldeiro
lealceldeiro

Reputation: 14958

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);

Upvotes: 2

Djaouad
Djaouad

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

Richie Bendall
Richie Bendall

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

Related Questions