JavaScript の文字列リテラル
シングルクォートあるいは、ダブルクォートで囲んだ文字列リテラルから、文字列値を生成することができます。 JavaScript の文字列変数は、基本的にこの形で生成すべきです。
const s1 = 'xxx';
const s2 = "yyy";
console.log(typeof(s1)); // "string"
console.log(typeof(s2)); // "string"
シングルクォートとダブルクォートのどちらで囲むかによっては違いは出ませんが、例えばダブルクォートで囲んだ場合は、内部にエスケープなしでシングルクォート文字を含められます。
const s1 = 'That\'s great!';
const s2 = "That's great!";
文字列を new String で作成するのは避ける
文字列を new String で作成すると、文字列の同値比較ができなくなってしまうので、文字列は必ず文字列リテラルで生成するようにすべきです。
これは、new String で生成した値の型が、string ではなく object 型になってしまうことが原因です。
const s1 = new String('ABC');
const s2 = new String('ABC');
console.log(s1 == s2); //=> false
console.log(s1 === s2); //=> false
const s1 = 'ABC';
const s2 = new String('ABC');
console.log(s1 == s2); //=> true
console.log(s1 === s2); //=> false
const s1 = 'ABC';
const s2 = 'ABC';
console.log(s1 == s2); //=> true
console.log(s1 === s2); //=> true