/

HTML / CSS / JavaScript Tutorial

JavaScript Operator: not equal

[this page | pdf | back links]

In JavaScript, the != operator is the ‘not equal to’ operator:

 

x

y

x != y

8

8

false

8

5

true

 

Note: if x and y are of different type then some type coercion will typically occur, and values that developers might not immediately recognise as ‘unequal’ may be deemed unequal by this operator. If you want only variables that are of the same type to pass the equality test then you should use the strictly unequal to operator, i.e. !==.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>table,th,tr,td {border: 1px solid black;
  border-collapse: collapse; text-align: center;}</style>
</head>
<body>
<table>
<tr>
<th>&nbsp;x&nbsp;</th><th>&nbsp;y&nbsp;</th>
<th><code>x != y</code></th>
</tr>
<tr>
<td><code>8</code></td><td><code>5</code></td>
<td><code id="A"></code></td>
</tr>
<tr>
<td><code>8</code></td><td><code>8</code></td>
<td><code id="B"></code></td>
</tr>
<tr>
</table>

<script>
document.getElementById("A").innerHTML = 8 != 5;
document.getElementById("B").innerHTML = 8 != 8;
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Operators


Desktop view | Switch to Mobile