I can't find documentation that says that Node treats files as encoded in UTF-8, but it seems that way experimentally:
/* Check in your editor that this Javascript file was saved in UTF-8 */
var nonEscaped = "Планета_Зямля";
var escaped = "\u041f\u043b\u0430\u043d\u0435\u0442\u0430\u005f\u0417\u044f\u043c\u043b\u044f";
if (nonEscaped === escaped) {
console.log("They match");
}
The above example prints They match
.
Non-BMP note:
Note that UTF-8 supports non-BMP code points (U+10000 and onwards), but Javascript has complications in that case, it automatically converts them to surrogate pairs. This is part of the language:
/* Check in your editor that this Javascript file was saved in UTF-8 */
var nonEscaped = ""; // U+1F4A9
var escaped1 = "\ud83d\udca9";
if (nonEscaped === escaped1) {
console.log("They match");
}
/* Newer implementations support this syntax: */
var escaped2 = "\u{1f4a9}";
if (nonEscaped === escaped2) {
console.log("The second string matches");
}
This prints They match
and The second string matches
.