LoginSignup
0
0

More than 3 years have passed since last update.

[js] 文字列への変換にnew String()を使うのはよくない

Posted at

こんなコードを見かけた

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ではない!

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0