Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the logic behind the output of console.log('\x') where x is an arbitrary number?

What is the logic behind the output of the following examples:

console.log('\272') // -> º

console.log('\364') // -> ô

As far as I know, \ is an escape character in javascript which means it tries to escape the following character but in the first example it is not equal to ASCII code of 72 which is character H.

like image 469
Amir J Avatar asked Mar 05 '23 01:03

Amir J


1 Answers

That's because of the octal encoding.

Any character with a character code lower than 256 (i.e. any character in the extended ASCII range) can be escaped using its octal-encoded character code, prefixed with . (Note that this is the same range of characters that can be escaped through hexadecimal escapes.)

To use the same example, the copyright symbol ('©') has character code 169, which gives 251 in octal notation, so you could write it as '\251'.

You can take a look to this explanation, quite illustrative: https://mathiasbynens.be/notes/javascript-escapes

like image 99
Jose A. Ayllón Avatar answered Apr 08 '23 22:04

Jose A. Ayllón