Updated June 8, 2023
Introduction to Unary Operators in JavaScript
JavaScript Unary Operators are the special operators that consider a single operand and perform all the operations on that single operand. These operators include unary plus, unary minus, prefix increments, postfix increments, prefix decrements, and postfix decrements. Unary operators in JavaScript can be placed before or after the operand depending on the requirement and desired output. They are more efficient in function calls compared to other types of operators. Additionally, they cannot be overridden, making their functionality flexible and versatile.
Types of JavaScript Unary Operators
Unlike normal Operators, which include addition, subtraction, multiplication, and division as part of the operation, make use of these types of general operators. Unary operators in JavaScript have specific rules and constraints that dictate how the operations are performed on a single operand.
The following are descriptions of the types of unary operators in JavaScript:
1. Unary plus(+)
This operator precedes the operand and converts the operator into the final output with the required number. It converts the string containing numbers, Boolean values, and null into the last number. It will include numbers like integers, float, hexadecimal, etc.
Note: Unary operator will return value only if the object it has contains the valueOf key, and that function, in turn, returns any of the values as mentioned above.
Code:
function M_Unary_Typ(p) {
this.number = p;
}
M_Unary_Typ.prototype.valueOf = function() {
return this.number;
};
const obj_1 = new M_Unary_Typ(9);
console.log(obj_1 + 3);
Output:
2. Unary Negation(-)
It also works similarly to Unary Plus with the mere difference that it includes the operand preceded with the operand and then converts the unary plus operand with the same function, which negates the value. In short, performing unary negation and unary plus on top of unary negation makes the value negated with the same function as non-numbers. This program demonstrates the Unary Negation operation, which gives the negative value once applied with the value of the key.
Code:
function M_negtion_func(s) {
this.nm = s;
}
M_negtion_func.prototype.valueOf = function() {
return this.nm;
};
const obj1 = new M_negtion_func(true);
const obj2 = new M_negtion_func(15);
console.log(obj1 + 3 -20);
console.log(obj2 + 3 -20);
Output:
3. Logical Not(!)
The logical Not operator comes with the fact that it comes before the operand and converts the operand into its boolean equivalent before converting it into the boolean equivalent and negating the value. This program demonstrates the Logical Not(!) unary operator, which returns the output with logical not.
Code:
function M_negtion_func(z) {
this.nm = z;
}
M_negtion_func.prototype.valueOf = function() {
return this.nm;
};
const obj1 = new M_negtion_func(!false);
const obj2 = new M_negtion_func(!"-3");
console.log(obj1, !false );
console.log(obj2, !"-3");
Output:
4. Increment(++)
The increment is also one type of unary operand that uses operators and operands where the operator adds one to its operand and returns the result as its value. Further, this operator can be categorized into two types, namely Postfix and Prefix. In postfix, the operator comes after the operand, thus returning the value before performing any increment. In prefix, the operator comes before the operand, thus producing the value after prefixing the value.
Code:
let q = 15;
++q;
console.log(q);
Output:
5. Decrement(–)
The decrement operator subtracts a value from one of its operands and returns a value before performing a postfix while performing a decrement operation. Performing prefixing before it returns a value and then decrementing makes it different. This program demonstrates the Decrement unary operator, which decrements the value of the prefixed value of the considered variable.
Code:
var k = 5
k = --k
console.log(k);
Output:
6. Bitwise not(~)
Bitwise is not performing a binary NOT operation by inverting all the bits in the operand and then returning a number with its value. This program demonstrated the Bitwise not(~), which reverses all the bits and returns the value shown in the output.
Code:
var i = ~8
var j = ~9
console.log(i);
console.log(j);
Output:
7. type of
It is a unary operand that returns a string indicating that the data type of the operand is a string that makes use of operators which come before the operand. This program demonstrates the kind of Date() functionality returning the date in the console using a unary operator.
Code:
typeof Date()
console.log(Date());
Output:
8. Delete
Delete is also one of the Unary operators in JavaScript, which makes use of the operators and operands where the delete operator also comes before the operand, and then deletes that specific index of an array and if there is any particular property of an object. It doesn’t hamper the state of the variable, function, or object anything; just some property or the defined thing gets deleted from this. This program demonstrates the delete unary operator with the output value as shown.
Code:
var welcm = 2;
delete welcm;
console.log(welcm);
Output:
9. void
The void operator precedes and is placed before an operation that discards any expression’s value. Then it evaluates the expression for returning some value with an undefined return type, which means the return type is empty and does not contain any specific matter for future reference. This program demonstrates the void unary operator’s output, as shown in the production.
Code:
void 0
var wl = function () {
console.log('welcome')
return 4;
}
var re_slt = wl()
console.log(re_slt);
var void_rstlt = void (wl())
console.log(void_rstlt);
Output:
Conclusion
Unary operators are special operators that use a single operand to perform logical manipulations and simplify operations. Although these operators and operations are performed on a single operand, they provide flexibility and versatility to the programming language.
Recommended Articles
We hope that this EDUCBA information on “Unary Operators in JavaScript” was beneficial to you. You can view EDUCBA’s recommended articles for more information.