/

HTML / CSS / JavaScript Tutorial

JavaScript Operator: minus

[this page | pdf | back links]

In JavaScript, the - operator has two possible meanings:

 

Arithmetic operator (binary)

 

Subtraction, e.g. if x is 8 then y = x - 2 results in y becoming 6

 

Arithmetic operator (unary)

 

Minus sign, e.g. if x is 8 then then -x represents -8

 

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;}</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="SubtractionExample"></code></td>
<td><code id="Subtraction"></code></td>
</tr>
<tr>
<td><code id="NegationExample"></code></td>
<td><code id="Negation"></code></td>
</tr>
</table>

<script>
document.getElementById("SubtractionExample").innerHTML =
  'var x = 8 - 2;';
document.getElementById("Subtraction").innerHTML = 8 - 2;
document.getElementById("NegationExample").innerHTML =
  'var x = -8;';
document.getElementById("Negation").innerHTML = -8;
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Operators


Desktop view | Switch to Mobile