小なり演算子 (<) は、左辺のオペランドが右辺のオペランドより小さい場合は true を返し、それ以外の場合は false を返します。
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.
構文
x < y
説明
オペランドは、以下に大まかに要約されている抽象関係比較アルゴリズムを使用して比較されます:
- 最初に、オブジェクトは
Symbol.ToPrimitiveを使用してプリミティブに変換されます。 - 両方の値が文字列である場合、それらに含まれる Unicode コードポイントの値に基づいて、文字列として比較されます。
- それ以外の場合、 JavaScript は非数値型を数値に変換しようとします:
- ブール値
trueおよびfalseは、それぞれ 1 および 0 に変換されます。 nullは 0 に変換されます。undefinedはNaNに変換されます。- 文字列は、含まれている値に基づいて変換され、数値が含まれていない場合は
NaNとして変換されます。
- ブール値
- いずれかの値が
NaNの場合、演算子はfalseを返します。 - それ以外の場合、値は数値として比較されます。
例
文字列と文字列の比較
console.log("a" < "b"); // true
console.log("a" < "a"); // false
console.log("a" < "3"); // false文字列と数値の比較
console.log("5" < 3); // false
console.log("3" < 3); // false
console.log("3" < 5); // true
console.log("hello" < 5); // false
console.log(5 < "hello"); // false
console.log("5" < 3n); // false
console.log("3" < 5n); // true数値と数値の比較
console.log(5 < 3); // false
console.log(3 < 3); // false
console.log(3 < 5); // true数値と BigInt の比較
console.log(5n < 3); // false
console.log(3 < 5n); // trueブール値, null, undefined, NaN の比較
console.log(true < false); // false
console.log(false < true); // true
console.log(0 < true); // true
console.log(true < 1); // false
console.log(null < 0); // false
console.log(null < 1); // true
console.log(undefined < 3); // false
console.log(3 < undefined); // false
console.log(3 < NaN); // false
console.log(NaN < 3); // false仕様
ブラウザー実装状況
BCD tables only load in the browser
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.