Unary Operators: Single-Operand Power
In JavaScript, unary operators are operators that take only one operand. They perform various operations like type conversion, negation, logical inversion, and determining data types. You've already encountered a couple of these (increment and decrement) in the Arithmetic Operators lesson!
1. Unary Plus (+)
The unary plus operator attempts to convert its operand into a number.
- If the operand is already a number, it returns the number itself.
- If the operand is a string that can be converted to a number, it performs the conversion.
- If the operand is
true, it converts to1. Iffalse, it converts to0. - If the operand cannot be converted to a number, it results in
NaN(Not-a-Number).
Examples:
2. Unary Minus (-)
The unary minus operator also attempts to convert its operand into a number, and then it negates the result.
- Behaves similarly to unary plus for type conversion.
- Then, it flips the sign of the resulting number.
Examples:
3. Increment (++) and Decrement (--)
You've already seen these in the Arithmetic Operators topic! They are unary operators because they modify a single variable by adding (++) or subtracting (--) 1 from its value.
++variable(Prefix): Increments, then returns the new value.variable++(Postfix): Returns the original value, then increments.--variable(Prefix): Decrements, then returns the new value.variable--(Postfix): Returns the original value, then decrements.
Quick Recap Example:
4. Logical NOT (!)
The logical NOT operator (!) takes a single operand and:
- Converts the operand to a boolean value (if it isn't already).
- Negates that boolean value.
It's often used to flip true to false and false to true, but also converts "truthy" values to false and "falsy" values to true.
Common use case: !! for explicit boolean conversion
Using the ! operator twice (!!) is a common trick to explicitly convert any value to its strict boolean equivalent (true or false) without negating it.
Examples:
5. typeof
The typeof operator returns a string indicating the data type of its operand. It's very useful for type checking in JavaScript.
Examples:
6. delete
The delete operator is used to remove a property from an object. It returns true if the deletion was successful or if the property didn't exist, and false if the property is non-configurable (cannot be deleted).
Important Notes:
deletedoes not delete variables (e.g., those declared withlet,const,var). It only affects object properties.- Attempting to delete a non-existent property returns
true(it "succeeded" in that the property is definitely not there).
Examples:
7. void
The void operator evaluates an expression and then returns undefined.
Its most common use case is in HTML javascript: URLs to prevent the browser from loading a new page or scrolling when a link is clicked, especially when the link is used to trigger JavaScript without navigation. void(0) is frequently seen for this purpose.
Examples:
Exercise: Unary Operator Challenge
In the editor below, use different unary operators to achieve the desired results and log them to the console.

