LoginSignup
0
0

More than 3 years have passed since last update.

JavaScript: 型変換を利用した簡易的な書き方

Last updated at Posted at 2021-01-25

数値型から文字列への変換

足し算は文字列連結の可能性と数値計算の可能性があるが、前者とみなされる。
const num = 999;
console.log(typeof num); //number
console.log(typeof num.toString()); //string
console.log(typeof (num + '')); //string

文字列から数値型への変換

引き算によって数値型に暗黙的に型変換される(文字列連結の可能性はないため?)
const str = '1000';
console.log(typeof str); //string
console.log(typeof Number(str)); //number
console.log(typeof parseInt(str)); //number
console.log(typeof +str); //number
console.log(typeof (str - 0)); //number
0
0
3

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