Introduction to Unary Operators in JavaScript
JavaScript Unary Operators are the special operators that consider a single operand and perform all the types of operations on that single operand. These types of operators include unary plus, unary minus, prefix increments, postfix increments, prefix decrements, and postfix decrements. These operators are placed before or after the operand depending on the requirement and the output. Unary Operators in comparison to JavaScript are more preferred once as they are more efficient in terms of function calls. Unary operators cannot get overridden which makes its functionality flexible and versatile.
Types of JavaScript Unary Operators
Unlike normal Operators which include addition, subtraction, multiplication, and division as part of the operation makes use of these types of general operators. But in the case of Unary Operators, some constraints and rules are applied based on which the operation takes place and includes a single operand for all manipulations.
Therefore, types of JavaScript Unary Operators are described as follows :
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 any type of null into the final number. It will include numbers like integer, float, hexadecimal, etc. In terms of JavaScript, it can be said that the Unary plus basically attempts to convert any expression to a number.
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 in a similar fashion as Unary plus with a mere difference that it includes the operand preceded with the operand and then converts the unary plus operand with the same function which makes the value as negated. In short performing unary negation and unary plus on top of unary negation makes the value negated with 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(!)
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 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 which makes use of operators and operands where the operator adds one to its operand and then returns the result as its value. Further, this operator can be categorized as 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 returning the value after prefixing the value.
Code:
let q = 15;
++q;
console.log(q);
Output:
5. Decrement(–)
The decrement operator subtracts value from one of its operands and returns a value before performing a postfix while performing 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 not is a kind of 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 inverts all the bits and then returning the value as shown in the output.
Code:
var i = ~8
var j = ~9
console.log(i);
console.log(j);
Output:
7. type of
type of is a unary operand which returns a string indicating that the data type of the operand is a string which makes use of operators which comes before the operand. This program demonstrates the type of Date() functionality returning the date in the console using type of 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 it deletes that specific index of an array and if there is any specific property of an object. It doesn’t hamper the state of the variable, function, or object anything just some property or the defined object 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 is an operator that precedes and is placed before an operation where it discards the value of any expression and then it evaluates the expression for returning some value with undefined return type which means the return type is empty and does not contain any specific value for future reference. This program demonstrates the void unary operator with its output as shown in the output.
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 the special operators which makes use of the operators and operands simultaneously but use of operand is single which means that one single operand handles all the operations and operators but the operations being performed on it is not what makes it flexible and versatile rather it makes the operations and logical manipulations more simplified.
Recommended Articles
This is a guide to Unary Operators in JavaScript. Here we discuss an introduction to Unary Operators in JavaScript, and types with programming example. You can also go through our other related articles to learn more –
39 Online Courses | 24 Hands-on Projects | 230+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses