JavaScript comparison



Comparison and logical operators are used to test true or false.


Comparison operators

Comparison operators are used in logical statements to determine whether variables or values ​​are equal.

x=5, the following table explains the comparison operators:

==:

Instance

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x==8 的比较值结果。</p>
<button onclick="myFunction()">尝试一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x==8;
}
</script>

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance

Instance

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x==5 的比较值结果。</p>
<button onclick="myFunction()">尝试一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x==5;
}
</script>

</body>
</html>

Run Instance»

Click "Run Instance" "Button to view online examples

===:

##Examples

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x==="5" 的比较值结果。</p>
<button onclick="myFunction()">尝试一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x==="5";
}
</script>

</body>
</html>

Run Instance»Click the "Run Instance" button to view the online instance

##Instance

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x===5 的比较值结果。</p>
<button onclick="myFunction()">尝试一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x===5;
}
</script>

</body>
</html>

Run Instance»
Click the "Run Instance" button to view the online instance

!=:

##Instance

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x!=8 的比较值结果。</p>
<button onclick="myFunction()">尝试一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x!=8;
}
</script>

</body>
</html>

Running Instance»

Click the "Run Instance" button to view the online instance

!==:

Instance

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x!=="5" 的比较值结果。</p>
<button onclick="myFunction()">尝试一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x!=="5";
}
</script>

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance

Instance

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x!==5 的比较值结果。</p>
<button onclick="myFunction()">尝试一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x!==5;
}
</script>

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance

>:

##Example

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x>8 的比较值结果。</p>
<button onclick="myFunction()">尝试一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x>8;
}
</script>

</body>
</html>

Run Instance»
Click the "Run Instance" button to view the online instance

<:

##Example

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>

<p>x=5, 返回  x<8 的比较值结果。</p>
<button onclick="myFunction()">尝试一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x<8;
}
</script>

</body>
</html>


Run Instance»

Click the "Run Instance" button to view the online instance

>=:

Instance

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x>=8 的比较值结果。</p>
<button onclick="myFunction()">尝试一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x>=8;
}
</script>

</body>
</html>

Run Instance»

Click the "Run Instance" button to view the online instance

<=:

##Example

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>

<p>x=5, 返回 x<=8 的比较值结果。</p>
<button onclick="myFunction()">尝试一下</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x=5;
	document.getElementById("demo").innerHTML=x<=8;
}
</script>

</body>
</html>

Run Instance»Click the "Run Instance" button to view the online instance


How to use

You can use comparison operators in conditional statements to compare values ​​and then take action based on the results:

if (age<18) x="Too young ";

#You will learn more about conditional statements in the next section of this tutorial.


Logical Operators

Logical operators are used to determine the logic between variables or values.

Given x=6 and y=3, the following table explains the logical operators:

Operators DescriptionExample&&and(x < 10 && y > 1 ) is true||or(x==5 || y==5) is false!not!(x==y) is true



Conditional Operator

JavaScript also includes conditional operators that assign values ​​to variables based on certain conditions.

Syntax

##variablename=(condition)?value1 :value2
Example


Example

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>

<p>点击按钮检测年龄。</p>
年龄:<input id="age" value="18" />
<p>是否达到投票年龄?</p>
<button onclick="myFunction()">点击按钮</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var age,voteable;
	age=document.getElementById("age").value;
	voteable=(age<18)?"年龄太小":"年龄已达到";
	document.getElementById("demo").innerHTML=voteable;
}
</script>

</body>
</html>

Run Instance»
Click the "Run Instance" button to view the online instance