単項プラス演算子 (+) は、オペランドの前に置かれ、そのオペランドを評価し、それが数値以外の場合は数値に変換します。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
例
Operator: +x
説明
単項マイナス (-) も非数値を数値に変換できますが、単項プラスは数値に対して他の演算を行わないため、非数値を数値に変換する最も高速で好ましい方法です。これは、整数や浮動小数点の文字列表現や、非文字列値である true、false、null を変換することができます。10進数と16進数(接頭辞 0x)の両形式の整数と負の数(16進数の負の数はサポートされていません)がサポートされています。BigInt 値に対してこの演算子を使用すると TypeError がスローされます。特定の値を解析できない場合は、NaN と評価されます。
例
数値での使い方
const x = 1; const y = -1; console.log(+x); // 1 console.log(+y); // -1
数値以外での使い方
+true // 1
+false // 0
+null // 0
+function(val){ return val } // NaN
+1n // BigInt 値は数値に変換できないためエラーになります
仕様
| 仕様書 |
|---|
| ECMAScript (ECMA-262) Unary plus operator の定義 |
ブラウザの互換性
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| デスクトップ | モバイル | サーバー | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Unary plus (+) | Chrome 完全対応 1 | Edge 完全対応 12 | Firefox 完全対応 1 | IE 完全対応 3 | Opera 完全対応 4 | Safari 完全対応 1 | WebView Android 完全対応 1 | Chrome Android 完全対応 18 | Firefox Android 完全対応 4 | Opera Android 完全対応 10.1 | Safari iOS 完全対応 1 | Samsung Internet Android 完全対応 1.0 | nodejs 完全対応 0.1.100 |
凡例
- 完全対応
- 完全対応