Table of Content
Unary operators in JavaScript work with only one value. They can change, test, or change the type of that value, and they are used in many different cases in programs.
Understand the Unary Operators in JavaScript
A unary operator is an operator that works with one operand only. This operand can be a number, a variable, or an expression. The operator then produces a new value.
A binary operator uses two operands, while a unary operator uses only one. For example, x + y is binary because it has two values, but -x is unary because it has one.
Unary operators in JavaScript include the following:
- Increment
- Decrement
- Unary plus
- Unary minus
- Logical NOT
- The
typeof voiddelete
Each one has a unique role. Here is a quick example:
let x = 5;
let y = -x;
console.log(y); // -5Here, the unary minus changes x from 5 to -5. It works on one value and gives its negative without changing the original variable.
Types of Unary Operators in JavaScript
Unary plus (+) helps you to change the operand to a number if it is not already.
Here is an example:
let str = "10";
let num = +str;
console.log(num); // 10The "10" is a string, but + changes it to a number, so num is numeric.
Unary minus (-) returns the negative version of the value. Here is an example:
let n = 8;
let result = -n;
console.log(result); // -8The operator takes the number 8 and returns -8.
Increment (++) adds one to the operand. For example:
let count = 3;
++count;
console.log(count); // 4The pre-increment adds one before the value is used.
Decrement (--) subtracts one from the operand. For example:
let items = 5;
--items;
console.log(items); // 4The value is reduced by one before it is read.
Logical NOT (!) changes a truthy value to false and a falsy value to true. Here is an example:
let isOpen = true;
console.log(!isOpen); // falseThe true becomes false because ! flips the value.
The typeof returns a string that tells the type of the value.
let value = "test";
console.log(typeof value); // "string"The typeof operator checks the type without changing the value.
The void runs an expression, but returns undefined.
void console.log("Hello");The expression runs, but its value is always undefined.
The delete removes a property from an object.
let obj = { name: "Ali" };
delete obj.name;
console.log(obj.name); // undefinedThe property name is removed from the object.
The Differences Between the Pre-Increment and Post-Increment Difference
The Pre-increment (++x) adds one to the value first, then returns the new value. Post-increment (x++) returns the current value first, then increments it by one.
Here is a table that shows you the key differences between them:
| Feature | Pre-increment (++x) | Post-increment (x++) |
|---|---|---|
| When value changes | Before it is returned | After it is returned |
| Returned value | New value | Original value |
| Typical usage | When you need the incremented value immediately | When you need the old value before incrementing |
| Effect in expressions | Uses updated value in the expression | Uses original value in the expression |
Examples
Use of Unary Plus with Boolean:
let isTrue = true;
let num = +isTrue;
console.log(num); // 1The unary plus changes true to the number 1. Boolean false would become 0. This is often used when you want quick number conversion for logic checks or counts.
Pre-increment in a Loop Condition:
let i = 0;
while (++i < 5) {
console.log(i);
}The loop adds one before checking if i is less than 5. This means it starts printing from 1 and stops before 5, as the increment happens before the comparison.
Using Logical NOT Twice for Conversion:
let value = "hello";
let isTrue = !!value;
console.log(isTrue); // trueTwo ! operators change a value to its boolean form. "hello" is truthy, so the result is true. This is a common way to ensure a strict boolean value.
Delete a Property in an Object:
let user = { name: "Sara", age: 22 };
delete user.age;
console.log(user); // { name: "Sara" }The delete operator removes age from the object. This is useful when you need to clean up objects before sending them or saving them.
Wrapping Up
In this article you learned what unary operators are and how each one works with only one operand. You also learned the difference between pre-increment and post-increment. Here is a quick recap:
- Unary operators work with one value only
- They can change values, check values, or change types
- Common operators are
+,-,++,--,!,typeof,void, anddelete - Pre-increment changes the value before it is used, post-increment after
FAQs
What are JavaScript unary operators?
- Increment (++) increases a number by one.
- Decrement (--) decreases a number by one.
- Unary plus (+) converts a value to a number.
- Unary negation (-) changes the sign of a number.
- Logical NOT (!) flips a boolean value.
- Bitwise NOT (~) inverts bits of a number.
- typeof returns the type of a value.
- delete removes a property from an object.
- void runs an expression without returning a value.
How does the increment operator work in JavaScript?
let num = 5; num++; // post-increment console.log(num); // 6 let x = 5; ++x; // pre-increment console.log(x); // 6Pre-increment changes the value before use, post-increment changes after.
What is the difference between unary plus and unary minus in JavaScript?
console.log(+"5"); // 5 console.log(-"5"); // -5 console.log(+true); // 1 console.log(-true); // -1
How does the typeof operator work in JavaScript?
console.log(typeof 123); // "number" console.log(typeof "hello"); // "string" console.log(typeof true); // "boolean" console.log(typeof undefined); // "undefined" console.log(typeof null); // "object" (legacy quirk)
Similar Reads
JavaScript works with different kinds of values. Each value has a type, known as a data type. These types help…
Data types in JavaScript help hold values and shape code rules. They set clear plans for text, numbers, and other…
The forEach loop in JavaScript allows you to go through each item in an array. You do not have to…
Object to primitive conversion in JavaScript turns objects into primitive values. It happens with operators, comparisons, and functions. What is…
This guide shows how toSorted function works in JavaScript with arrays. It covers syntax, rules, and examples for numbers, text,…
AJAX keeps your web app fast. You do not need to reload the page every time you send or get…
Click a button on a website. Something changes. A menu opens or a message pops up. That’s how JavaScript works.…
JavaScript code follows some rules that help both new and old developers. A project without rules in code creates trouble…
The current essay is devoted to the basic principles and introduction of JavaScript. This language no longer needs to be…
JavaScript runs code in different ways, but the for loop stays one of the most common tools. You use it…