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


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

URL: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Division

href="?u=http://developer.mozilla.org/static/client/inter-latin.9a3b1bc220d426ef.woff2" as="font" type="font/woff2" crossorigen="anonymous" fetchpriority="low" />

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

View in English Always switch to English

除算演算子 (/)

Baseline 広く利用可能

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

除算演算子 (/) は、左のオペランドを被除数とし、右のオペランドを除数としたオペランド同士の除算結果を生成します。

試してみましょう

console.log(12 / 2);
// 予想される結果: 6

console.log(3 / 2);
// 予想される結果: 1.5

console.log(6 / "3");
// 予想される結果: 2

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

構文

js
x / y

解説

/ 演算子は、数値と長整数の 2 種類のオペランドに対してオーバーロードされています。まず両オペランドを数値型に変換し、それらの型を検査します。両オペランドが長整数になった場合、長整数の除算を実行します。そうでない場合は数値の除算を実行します。一方のオペランドが長整数になり、もう一方が数値になった場合は、TypeError が発生します。

長整数の除算では、結果は両オペランドの商を 0 方向に切り捨てた値となり、余りは破棄されます。除数 y0n の場合、RangeError が発生します。これは数値除算で 0 を割ると Infinity または -Infinity を返すのに対し、長整数には無限大の概念が存在しないためです。

基本的な除算

js
1 / 2; // 0.5
Math.floor(3 / 2); // 1
1.0 / 2.0; // 0.5

2 / 0; // Infinity
2.0 / 0.0; // Infinity, because 0.0 === 0
2.0 / -0.0; // -Infinity

長整数以外の値は数値に変換されます。

js
5 / "2"; // 2.5
5 / "foo"; // NaN

長整数を使用した除算

js
1n / 2n; // 0n
5n / 3n; // 1n
-1n / 3n; // 0n
1n / -3n; // 0n

2n / 0n; // RangeError: BigInt division by zero

除算で長整数と数値のオペランドを混在させることはできません。

js
2n / 2; // TypeError: Cannot mix BigInt and other types, use explicit conversions
2 / 2n; // TypeError: Cannot mix BigInt and other types, use explicit conversions

長整数と長整数以外で除算を行うには、どちらかのオペランドを変換してください。

js
2n / BigInt(2); // 1n
Number(2n) / 2; // 1

仕様書

仕様書
ECMAScript® 2027 Language Specification
# sec-multiplicative-operators

ブラウザーの互換性

関連情報

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