pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Unary_plus

="module">

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

単項プラス演算子 (+)

Baseline 広く利用可能

この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2015年7月以降、すべてのブラウザーで利用可能です。

単項プラス演算子 (+) は、オペランドの前に置かれ、そのオペランドを評価し、それが数値以外の場合は数値へ変換しようとします。

試してみましょう

const x = 1;
const y = -1;

console.log(+x);
// 予想される結果: 1

console.log(+y);
// 予想される結果: -1

console.log(+"");
// 予想される結果: 0

console.log(+true);
// 予想される結果: 1

console.log(+false);
// 予想される結果: 0

console.log(+"hello");
// 予想される結果: NaN

構文

js
+x

解説

単項マイナス (-) も非数値を数値に変換できますが、単項プラスは数値に対して他の演算を行わないため、非数値を数値に変換する最も高速で好ましい方法です。

単項プラス演算子は、数値を期待するほとんどの組み込みメソッドで使用される通常の数値の型変換と全く同じ段階を踏みます。整数や浮動小数点の文字列表現、および非文字列値である truefalsenull を変換することができます。 10 進数および 16 進数(0x接頭辞付き)形式の整数に対応しています。負の数値も対応しています(ただし 16 進数は除く)。特定の値を解釈できない場合、NaN と評価されます。数値と長整数の両方で動作する他の算術演算子とは異なり、長整数値に対して + 演算子を使用すると TypeError が発生します。

数値での使い方

js
const x = 1;
const y = -1;

console.log(+x);
// 1
console.log(+y);
// -1

数値以外での使い方

js
+true  // 1
+false // 0
+null  // 0
+[]    // 0
+function (val) { return val; } // NaN
+1n    // TypeError: Cannot convert BigInt value to number が発生

仕様書

仕様書
ECMAScript® 2027 Language Specification
# sec-unary-plus-operator

ブラウザーの互換性

関連情報

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy