Introduction
This article will cover the use of two topics in JavaScript, i.e., Unary Operators and Pure functions. Both have different individuality in terms of providing easiness in the code.
Also Read, Javascript hasOwnProperty
Unary Operators
You might have listened in other languages regarding the unary operators, which means operation on a single operand.
Let's go through some of these operators.
Unary plus (+)
The unary plus operator is a simple plus sign (+). If you place the unary plus(+) before a numeric value, let's see what it does through the example below.
For example
let x = 65;
let y = +x;
console.log(y);
Output:
65
let fl = false,
tl = true;
console.log(+tl);
console.log(+fl);
Output:
1
0
Suppose you have an object with the toString() method as follows:
let boy = {
name: 'Harry',
toString: function () {
return '30';
},
};
console. log(+boy);
Output:
30
After experimenting with different values in the above examples, you can continue to the next unary operator.
Unary minus (-)
The unary minus operator is denoted by a single minus sign (-). If you apply the unary minus operator to a numeric value, it negates the number.
For example:
let x = 5;
let y = -x;
console.log(y);
Output
-5
Suppose you apply the unary minus operator(-) to a non-numeric value. It converts the value into a number applying the same rules as the unary plus operator and then negates the value.
Increment (++)
The increment operator (++) increments its operand and returns the incremented value.
It can be used as a prefix operator or postfix operator.
- Postfix: It means that the operator comes after the operand like this (y++). This returns the value before incrementing.
- Prefix: The operator comes before the operand like this (++y). While using it as a prefix returns the value after incrementing.
Example of the postfix operator:
x = 6 // x = 6
y = x++ // y = 6 and x = 7
Here is a prefix example:
IIx = 4 // x = 4
y = ++x // y = 5 and x = 5
After experimenting with different values in the above example, you can continue to the next unary operator.
Decrement (--)
The decrement operator (--) decrements its operand and returns a value.
It can be used as a prefix operator or postfix operator.
- Postfix means that the operator comes after the operand (y--). This returns the value before decrementing.
- Prefix means that the operator comes before the operand (--y). while using it as a prefix returns the value after decrementing.
Here is a postfix example:
x = 6 // x = 6
y = x-- // y = 6 and x = 5
Here is a prefix example:
x = 4 // x = 4
y = --x // y = 3 and x = 3
After experimenting with different values in the above example, continue to the next unary operator.
Logical NOT (!)
The logical NOT (!) operator performs negation on the given expression; It takes truth to falsity and vice versa.
These examples below demonstrate how logical NOT returns false if the operand can be converted to true. If not, it returns false.
You can also use double negation (!!).
!!1 === true // returns true
!!0 === false // returns true
Let's examine the last example.
First, a negation of a string('0') returns false:
Then, a negation of false, returns true:
!false // returns true
Thus:
true === true // returns true
After experimenting with different values in the above examples, you can continue to the next unary operator.
Here is an image that depicts the order of precedence in operations when using Javascript.