Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Bootcamps Get Certified Upgrade Teachers Spaces Bootcamps
   ❮     
     ❯   

Basic JavaScript

JS TutorialJS SyntaxJS VariablesJS OperatorsJS If ConditionsJS LoopsJS StringsJS NumbersJS FunctionsJS ObjectsJS ScopeJS DatesJS Temporal DatesJS ArraysJS SetsJS MapsJS IterationsJS MathJS RegExpJS DestructuringJS Data TypesJS ErrorsJS DebuggingJS ConventionsJS ReferencesJS 2026JS Versions

JS HTML

JS HTML DOMJS EventsJS ProjectsNew

JS Advanced

JS FunctionsJS ObjectsJS ClassesJS AsynchronousJS ModulesJS Meta & ProxyJS Typed ArraysJS DOM NavigationJS WindowsJS Web APIsJS AJAXJS JSONJS jQueryJS GraphicsJS ExamplesJS Reference


JavaScript Comparison

Comparison Operators

Comparison operators are used tocompare two values.

Comparison operators always returntrue orfalse.

Given thatx = 5, the table below explains the comparison operators:

OperatorDescriptionComparingReturns
==equal tox == 8falseTry it »
x == 5trueTry it »
x == "5"trueTry it »
===equal value and equal typex === 5trueTry it »
x === "5"falseTry it »
!=not equalx != 8trueTry it »
!==not equal value or not equal typex !== 5falseTry it »
x !== "5"trueTry it »
x !== 8trueTry it »
>greater thanx > 8falseTry it »
<less thanx < 8trueTry it »
>=greater than or equal tox >= 8falseTry it »
<=less than or equal tox <= 8trueTry it »

Comparison operators can be used in conditional statements to compare values and take action depending on the result:

if (age < 18) text = "Too young to buy alcohol";

You will learn more about the use of conditional statements in theif...else chapter of this tutorial.



JavaScript String Comparison

All the comparison operators above can also be used on strings:

Example

let text1 = "A";
let text2 = "B";
let result = text1 < text2;
Try it Yourself »

Note that strings are compared alphabetically:

Example

let text1 = "20";
let text2 = "5";
let result = text1 < text2;
Try it Yourself »

Comparing Different Types

Comparing data of different types may give unexpected results.

When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison. An empty string converts to 0. A non-numeric string converts toNaN which is alwaysfalse.

CaseValueTry
2 < 12trueTry it »
2 < "12"trueTry it »
2 < "John"falseTry it »
2 > "John"falseTry it »
2 == "John"falseTry it »
"2" < "12"falseTry it »
"2" > "12"trueTry it »
"2" == "12"falseTry it »

When comparing two strings, "2" will be greater than "12".

Alphabetically 1 is less than 21.

To secure a proper result, variables should be converted to the proper type before comparison:

Example

age = Number(age);
if (isNaN(age)) {
  voteable = "Input is not a number";
} else {
  voteable = (age < 18) ? "Too young" : "Old enough";
}
Try it Yourself »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.

-->
[8]ページ先頭

©2009-2026 Movatter.jp