Unary operators in JavaScript are operators that work with only one operand. They are used to perform operations such as changing a value, converting a value into a number, checking its data type, or reversing a Boolean value. Unlike binary operators, which require two operands, unary operators operate on a single value or variable. Common unary operators in JavaScript include unary plus (+), unary minus (-), increment (++), decrement (--), logical NOT (!), bitwise NOT (~), typeof, void, and delete.
Table of Contents
What Are Unary Operators in JavaScript?
Unary operators are operators that require only one operand to perform an operation. The operand can be a variable, value, or expression.
For example, the unary minus operator changes the sign of a number.
Syntax:
operator operand
Example:
// JavaScript program to demonstrate a unary operator let number = 10; console.log(-number);
Output:
-10
Here, the - operator works on only one operand, which is number.
Types of Unary Operators
JavaScript provides several unary operators:
| Operator | Name | Description |
|---|---|---|
| + | Unary Plus | Converts a value to a number. |
| - | Unary Minus | Converts a value to a number and changes its sign. |
| ++ | Increment | Increases a value by 1. |
| -- | Decrement | Decreases a value by 1. |
| ! | Logical NOT | Reverses a Boolean value. |
| ~ | Bitwise NOT | Inverts the bits of a 32-bit integer. |
| typeof | Type Operator | Returns the data type of a value. |
| void | Void Operator | Evaluates an expression and returns undefined. |
| delete | Delete Operator | Removes a property from an object. |
Unary Plus (+)
The unary plus operator + is placed before a single value or variable. It attempts to convert the value into a number.
Syntax:
+value
Example:
// JavaScript program to demonstrate the unary plus operator let value = "25"; console.log(+value);
Output:
25
The value "25" is originally a string. The unary plus operator converts it into the number 25.
It can also be used with Boolean values.
// JavaScript program to demonstrate unary plus with Boolean values let firstValue = true; let secondValue = false; console.log(+firstValue); console.log(+secondValue);
Output:
1
0
JavaScript converts true to 1 and false to 0.
Unary Minus (-)
The unary minus operator - converts a value to a number and changes its sign.
Syntax:
-value
Example:
// JavaScript program to demonstrate the unary minus operator let number = 20; console.log(-number);
Output:
-20
The unary minus operator changes the positive value 20 into -20.
It can also convert a numeric string into a number while changing its sign.
// JavaScript program to demonstrate unary minus with a string let value = "15"; console.log(-value);
Output:
-15
Increment Operator (++)
The increment operator ++ increases the value of a variable by 1.
Syntax:
variable++
or
++variable
The increment operator has two forms: postfix increment and prefix increment.
Example:
// JavaScript program to demonstrate the increment operator let count = 5; count++; console.log(count);
Output:
6
The value of count increases from 5 to 6.
Decrement Operator (--)
The decrement operator -- decreases the value of a variable by 1.
Syntax:
variable--
or
--variable
Example:
// JavaScript program to demonstrate the decrement operator let count = 5; count--; console.log(count);
Output:
4
The value of count decreases from 5 to 4.
Logical NOT (!)
The logical NOT operator! reverses a Boolean value. It changes true to false and false to true.
Syntax:
!value
Example:
// JavaScript program to demonstrate the logical NOT operator let isLoggedIn = true; console.log(!isLoggedIn);
Output:
false
Since isLoggedIn is true, the! operator reverses the value to false.
The logical NOT operator also performs Boolean conversion before reversing the result.
// JavaScript program to demonstrate logical NOT with a value let value = 0; console.log(!value);
Output:
true
The number 0 is a falsy value in JavaScript, so applying ! to it produces true.
Bitwise NOT (~)
The bitwise NOT operator ~ inverts every bit of a 32-bit integer. It changes each 0 bit to 1 and each 1 bit to 0.
Syntax:
~value
Example:
// JavaScript program to demonstrate the bitwise NOT operator let number = 5; console.log(~number);
Output:
-6
JavaScript uses signed 32-bit integers for bitwise operations. The bitwise NOT operation follows this rule:
~n = -(n + 1)
Therefore:
~5 = -(5 + 1) = -6
typeof Operator
The typeof operator is used to determine the data type of a value.
Syntax:
typeof value
Example:
// JavaScript program to demonstrate the typeof operator let name = "Tanavi"; let age = 20; console.log(typeof name); console.log(typeof age);
Output:
string
number
The typeof operator is useful when checking the type of a variable or value.
void Operator
The void operator evaluates an expression but always returns undefined.
Syntax:
void expression
Example:
// JavaScript program to demonstrate the void operator let result = void 0; console.log(result);
Output:
undefined
The void operator is less commonly used in everyday JavaScript programming. It can be useful when an expression needs to be evaluated without returning its result.
delete Operator
The delete operator is used to remove a property from an object.
Syntax:
delete object.property
Example:
// JavaScript program to demonstrate the delete operator
let student = {
name: "Tanavi",
age: 20
};
delete student.age;
console.log(student);
Output:
{ name: 'Tanavi' }
The delete operator removes the age property from the student object.
Prefix and Postfix Operators
The increment and decrement operators can be used in prefix and postfix forms. The difference is important when the value is used in an expression.
Prefix Increment:
++variable
The value is increased first, and then the updated value is used.
Example:
// JavaScript program to demonstrate prefix increment let number = 5; let result = ++number; console.log(result); console.log(number);
Output:
6
6
In this example, number is increased to 6 before its value is assigned to result.
Postfix Increment:
variable++
The original value is used first, and then the variable is increased.
Example:
// JavaScript program to demonstrate postfix increment let number = 5; let result = number++; console.log(result); console.log(number);
Output:
5
6
Here, the original value 5 is assigned to result, and then number becomes 6.
Unary Operators with Expressions
Unary operators can also be applied to expressions. The operator works on the result of the expression.
Example:
// JavaScript program to demonstrate a unary operator with an expression let a = 10; let b = 5; console.log(-(a - b));
Output:
-5
The expression a - b produces 5, and the unary minus operator changes it to -5.
Complete Program Using Unary Operators
The following program demonstrates several commonly used unary operators.
// JavaScript program to demonstrate different unary operators
let number = 10;
let text = "25";
let isActive = true;
console.log("Unary Plus:", +text);
console.log("Unary Minus:", -number);
console.log("Logical NOT:", !isActive);
console.log("Bitwise NOT:", ~number);
console.log("Type:", typeof number);
number++;
console.log("After Increment:", number);
number--;
console.log("After Decrement:", number);
Output:
Unary Plus: 25
Unary Minus: -10
Logical NOT: false
Bitwise NOT: -11
Type: number
After Increment: 11
After Decrement: 10
Advantages of Unary Operators
- Simple Operations: Unary operators perform operations on a single value, making expressions concise and easy to write.
- Type Conversion: Unary plus and unary minus can convert suitable values into numbers.
- Value Modification: Increment and decrement operators make it easy to increase or decrease numeric variables.
- Boolean Evaluation: The logical NOT operator helps reverse or evaluate Boolean conditions.
- Type Checking: The typeof operator allows programs to determine the data type of a value.
Common Beginner Mistakes
- Confusing Unary and Binary Operators: A unary operator works with one operand, while many other operators require two operands.
- Confusing Prefix and Postfix Increment: ++variable updates the value before it is used, while variable++ uses the original value before updating it.
- Expecting Unary Plus to Always Produce a Valid Number: Applying unary plus to a non-numeric string produces NaN.
- Confusing ! and ~: The! operator performs logical NOT, while ~ performs bitwise NOT.
- Using delete on a Variable: The delete operator is primarily used to remove properties from objects and should not be treated as a general-purpose variable removal operator.
Conclusion
Unary operators in JavaScript work with a single operand and are used for various operations such as type conversion, value modification, Boolean reversal, bit manipulation, and type checking. Operators such as unary plus, unary minus, increment, decrement, logical NOT, bitwise NOT, typeof, void, and delete each perform different tasks. Understanding these operators helps developers write shorter and more effective JavaScript expressions.
FAQs
1. What are unary operators in JavaScript?2. What is the difference between unary plus and unary minus?Unary operators are operators that work with only one operand. They can be used for operations such as type conversion, incrementing values, and checking data types.
3. What is the difference between prefix and postfix increment?Unary plus attempts to convert a value into a number, while unary minus converts the value into a number and changes its sign.
4. What does the typeof operator do in JavaScript?Prefix increment increases the value before it is used in an expression, while postfix increment uses the original value first and increases it afterward.
5. What does the delete operator do in JavaScript?The typeof operator returns a string that identifies the data type of a value.
The delete operator removes a property from an object and returns a Boolean result indicating whether the deletion was successful.
0 Comments