こんなコードを見かけた
console.log('price = ' + new String(price));
数値から文字列の変換にnew String
を用いている。
実はこのnew Stringが返すのはstringではなくstringっぽい何か
なのだ!
const s = '100';
const s2 = new String(100);
console.log(typeof s); // string
console.log(typeof s2); // object
実はnew String
が返すのはstringのボックス型なのだ。似たような振る舞いをするが決してstringではない!