A unary operator is an operator that works on a single value. While most operators you encounter in programming need two values (like 2 + 3), a unary operator only needs one. It can negate a number, flip a true to false, increment a counter, or even convert one data type to another.
The name comes from “unary” meaning “one,” which distinguishes these operators from binary operators (two operands, like addition or multiplication) and the ternary operator (three operands, used for conditional expressions).
Common Unary Operators
Most programming languages share a core set of unary operators, each represented by a symbol placed directly before or after a value:
- Unary minus (-) flips a number’s sign. If x is 5, then -x is -5.
- Unary plus (+) leaves a number’s sign unchanged but can trigger type conversion in some languages.
- Logical NOT (!) reverses a boolean value. If something is true, !true becomes false, and vice versa.
- Bitwise NOT (~) flips every individual bit in a number’s binary representation, turning all 0s to 1s and all 1s to 0s.
- Increment (++) adds 1 to a value.
- Decrement (–) subtracts 1 from a value.
Some languages have additional keyword-based unary operators. JavaScript, for example, includes typeof (which tells you a value’s data type), delete (which removes a property from an object), and void. These look different from symbols like ! or -, but they follow the same pattern: one operator, one operand.
How Negation Works
Arithmetic negation is the most intuitive unary operator. Placing a minus sign before a value produces its negative. If you write -x when x equals 7, the result is -7. Apply it again and -(-7) gives you 7. The operator doesn’t change the original variable; it produces a new value.
Logical negation does the same thing to boolean values. The ! operator reverses true to false and false to true. Applying it twice returns you to the original value, just like double-negating a number. This is useful when you need to check that a condition is NOT met, such as if (!loggedIn).
Prefix vs. Postfix Operators
Increment (++) and decrement (--) operators are unique because they can be placed either before or after the variable, and the position changes what happens during evaluation.
With prefix placement (++i), the value increases first, and then the new value is used in the expression. With postfix placement (i++), the original value is used in the expression first, and the increase happens afterward. A concrete example makes this clearer:
If a starts at 0 and you write b = ++a, both a and b end up as 1 because the increment happens before the assignment. But if you write b = a++ with a starting at 0, then b gets the original value of 0, while a becomes 1 afterward.
This distinction matters most when you use increment or decrement inside a larger expression, like a function call or an array index. In a standalone statement where you just write i++; on its own line, the prefix and postfix forms produce the same end result.
Type Conversion With Unary Plus
In JavaScript, the unary plus operator does something beyond simple arithmetic: it converts non-number values into numbers. Placing + before a string like "42" produces the number 42. It works on the strings “true” and “false” concepts too: +true evaluates to 1, +false to 0, and +null to 0.
If the string can’t be meaningfully converted, you get NaN (Not a Number). So +"" gives you 0, but +"hello" gives you NaN. This is a compact alternative to calling explicit conversion functions, though it can make code harder to read if used carelessly.
Bitwise NOT and Binary Representation
The tilde (~) operator works at the bit level. Every number your computer stores is ultimately a sequence of 0s and 1s, and the bitwise NOT flips each one. A 0 becomes 1, a 1 becomes 0.
Because of how computers represent negative numbers (a system called two’s complement), the bitwise NOT of any integer n produces -(n + 1). So ~9 gives you -10, and ~0 gives you -1. This operator shows up in low-level programming, bit manipulation tasks, and occasionally as a shorthand trick in JavaScript for checking whether a value exists in a string or array.
Pointer Operators in C and C++
Lower-level languages like C and C++ have two additional unary operators for working directly with memory. The address-of operator (&) gives you the memory address where a variable is stored. The indirection (or dereference) operator (*) does the reverse: given a memory address, it retrieves the value stored there.
These two operators are complementary. If you take the address of a variable with &x, you get a pointer. If you then dereference that pointer with *ptr, you get back the original value. This is fundamental to how C manages data structures, function arguments, and dynamic memory, but it doesn’t appear in higher-level languages like Python or JavaScript, which handle memory automatically.
Python’s Approach
Python supports four unary operators: arithmetic negation (-a), unary plus (+a), bitwise inversion (~a), and logical negation. The key difference from C-style languages is that Python spells out its logical NOT as the word not instead of !. Python also doesn’t have ++ or -- operators at all. If you want to increment a variable, you write x += 1.
Python also lets you define how unary operators behave on your own custom objects by implementing special methods in your class, which gives you flexibility that most languages don’t offer at this level.
Operator Precedence
Unary operators are evaluated before binary operators. In standard precedence tables for C-style languages, unary operators like +, -, !, ~, ++, and -- sit at precedence level 14, well above multiplication and division (level 12) and addition and subtraction (level 11).
This means an expression like -x * y always negates x first, then multiplies. You rarely need to think about this because it matches natural intuition. But when unary operators interact with other high-precedence operations, or when you chain multiple unary operators, parentheses make your intent clearer. Unary operators also associate right-to-left, so !!x evaluates the inner ! first, then the outer one.

