Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is Unary Negation Operator (-) in JavaScript?
The Unary Negation Operator (-) converts its operand to a number and then negates it. It operates on a single operand and returns the negative value. Boolean values are converted to 0 or 1 before negation, and numbers in different bases are converted to decimal first.
Syntax
-operand
The unary negation operator (-) precedes the operand to negate its value.
Basic Examples
Let's see how the unary negation operator works with different data types:
<html>
<body>
<script>
var a = true;
var b = '0xFF';
var c = false;
var d = 100;
var linebreak = "<br />";
document.write("-true = " + (-a));
document.write(linebreak);
document.write("-'0xFF' = " + (-b));
document.write(linebreak);
document.write("-false = " + (-c));
document.write(linebreak);
document.write("-100 = " + (-d));
</script>
</body>
</html>
-true = -1 -'0xFF' = -255 -false = -0 -100 = -100
Type Conversion Behavior
The operator first converts the operand to a number, then applies negation:
<html>
<body>
<script>
var str = "42";
var bool = true;
var hex = "0x10";
document.write("Original: " + str + " (type: " + typeof str + ")<br>");
document.write("Negated: " + (-str) + " (type: " + typeof(-str) + ")<br><br>");
document.write("Original: " + bool + " (type: " + typeof bool + ")<br>");
document.write("Negated: " + (-bool) + " (type: " + typeof(-bool) + ")<br><br>");
document.write("Original: " + hex + " (type: " + typeof hex + ")<br>");
document.write("Negated: " + (-hex) + " (type: " + typeof(-hex) + ")<br>");
</script>
</body>
</html>
Original: 42 (type: string) Negated: -42 (type: number) Original: true (type: boolean) Negated: -1 (type: number) Original: 0x10 (type: string) Negated: -16 (type: number)
Interactive Example
<html>
<body>
<h2>Unary Negation Operator (-) Demo</h2>
<p>Enter any value:</p>
<input type="text" id="inp" placeholder="Try: 42, true, 0xFF" /><br><br>
<button onclick="negateValue()">Negate Value</button>
<p id="result"></p>
<script>
function negateValue() {
var input = document.getElementById("inp").value;
var negated = -input;
var result = document.getElementById("result");
result.innerHTML =
"Original: " + input + " (type: " + typeof input + ")<br>" +
"Negated: " + negated + " (type: " + typeof negated + ")<br>" +
"Conversion: " + input + " ? " + (+input) + " ? " + negated;
}
document.getElementById("inp").addEventListener("keypress", function(e) {
if (e.key === "Enter") {
negateValue();
}
});
</script>
</body>
</html>
Special Cases
Here are some important edge cases to understand:
<html>
<body>
<script>
document.write("Negating non-numeric strings:<br>");
document.write("-'hello' = " + (-'hello') + "<br>");
document.write("-'' = " + (-'') + "<br>");
document.write("-null = " + (-null) + "<br>");
document.write("-undefined = " + (-undefined) + "<br><br>");
document.write("Double negation:<br>");
document.write("--5 = " + (--5) + "<br>");
document.write("--'10' = " + (--'10') + "<br>");
</script>
</body>
</html>
Negating non-numeric strings: -'hello' = NaN -'' = -0 -null = -0 -undefined = NaN Double negation: --5 = 5 --'10' = 10
Conversion Table
| Input Type | Example | Converted to Number | Negated Result |
|---|---|---|---|
| Boolean true | -true | 1 | -1 |
| Boolean false | -false | 0 | -0 |
| Numeric string | -"42" | 42 | -42 |
| Hex string | -"0xFF" | 255 | -255 |
| Empty string | -"" | 0 | -0 |
| Non-numeric string | -"abc" | NaN | NaN |
Conclusion
The unary negation operator (-) is a powerful tool that converts values to numbers and negates them. It's particularly useful for type conversion and mathematical operations, but be aware of how it handles non-numeric values like strings and booleans.
