1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

'10' - 1 = 9 だけど '10' + 1 = '101' になる

Posted at

jsで演算していて
おや?っとなったことがあったのでメモ。

string + number は string


function typeOf(value) {
  'use strict';
  console.log(typeof value);
}

var num = '10'
var result = num + 1
console.log(result) // '101'
typeOf(result) // string

string - number は number


function typeOf(value) {
  'use strict';
  console.log(typeof value);
}

var num = '10'
var result = num - 1
console.log(result) // 9
typeOf(result) // number

まとめ

冷静になって考えればそりゃそうなんだけど、演算子で変わるのはちょっと面白かった

1
1
0

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?