What are Unary Uperators in JavaScript ?
JavaScript Unary Operators work on a single operand and perform various operations, like incrementing/decrementing, evaluating data type, negation of a value, etc.
Types of Unary Operators
- Unary Plus Operator ( + )
- Unary Minus Operator ( – )
- Unary Increment Operator ( ++ )
- Unary Decrement Operator ( — )
- Logical NOT Operator ( ! )
- Bitwise NOT Operator ( ~ )
- typeof Operator
- delete Operator
- void Operator
We will explore all the above operators along with their basic implementation with the help of examples.
JavaScript Unary Plus (+) Operator
The unary plus (+) converts an operand into a number, if possible. It is commonly used to ensure numerical operations on variables that may contain numeric strings. If the operand is a string that represents a valid number, it will be converted to a number. Otherwise, it will evaluate to NaN (Not-a-Number).
Syntax:
+operand
Example: In this example, the unary plus converts the string “12” into the number 12. In the second case, the unary plus attempts to convert the string “Geeks” into a number, but since it’s not a valid number, it results in NaN
Javascript
let str1 = "12" ; // Using unary plus to convert string to number let num = +str1; console.log(num); // Here we are using typeof operator console.log( typeof (num)) // "Geeks" cannot be converted to a number let str2 = + "Geeks" ; console.log(str2); |
12 number NaN
JavaScript Unary Minus (-) Operator
The Unary Negation (-) operator is used to convert its operand to a negative number if it isn’t already a negative number.
Syntax:
-operand
Example: In this example, we are using unary negation to negate the given value.
Javascript
let str1 = "12" ; // Unary negation, negates the // value of number let num = -str1; console.log(num); // Unary negation, tries to convert // 'Geeks' to a number let str2 = - "Geeks" ; console.log(str2); |
-12 NaN
JavaScript Unary Increment (++) Operator
The unary increment operator (++) adds 1 to its operand’s value and evaluates to the updated value. It can be used as a postfix or prefix operator.
- Postfix: In postfix, the current value of the variable is used in the expression, and then the variable’s value is incremented by 1.
- Prefix: In prefix, the variable’s value is first incremented by 1, and then the updated value is used in the expression.
Syntax:
++operand // or
operand++
Example: In both cases, the variable is incremented by 1, but the order of evaluation differs between postfix and prefix forms.
Javascript
// Case 1: Postfix let num = 12; // The current value of 'num' (12) is used, // then 'num' is incremented to 13. let numPostfix = num++; console.log(numPostfix); // Updated value of 'num' console.log(num); // Case 2: Preifix let num1 = 10; //'num1' is incremented to 11, and then // the updated value (11) is used let num1Prefix = ++num1; console.log(num1Prefix); // Updated value of 'num1' console.log(num1); |
12 13 11 11
JavaScript Unary Decrement (–) Operator
The unary decrement operator (–) subtracts 1 from its operand’s value and evaluates it to the updated value, and we can use it as a postfix or prefix operator.
- Postfix: In postfix form, the current value of the variable is used in the expression, and then the variable’s value is decremented by 1.
- Prefix: In prefix form, the variable’s value is first decremented by 1, and then the updated value is used in the expression.
Syntax:
--operand // or
operand--
Example: In both cases, the variable is decremented by 1, but the order of evaluation differs between postfix and prefix forms.
Javascript
let num1 = 8; // The current value of 'num1' (8) is used, // then 'num1' is decremented to 7 let num1Postfix = num1--; // Output: 8 console.log(num1Postfix); // Output: 7 (updated value of 'num1') console.log(num1); let num2 = 15; // 'num2' is decremented to 14, and then // the updated value (14) is used let num2Prefix = --num2; // Output: 14 console.log(num2Prefix); // Output: 14 (updated value of 'num2') console.log(num2); |
8 7 14 14
JavaScript Logical NOT (!) Operator
The logical NOT (!) is a unary operator that negates the Boolean value of an operand, converting true to false and false to true.
Syntax:
!value
Example: In the first example, var1 is initially set to false, and then the logical NOT operator negates its value, resulting in result1 being set to true. In the second example, var2 is initially set to true, and the logical NOT operator negates its value, setting result2 to false.
Javascript
let var1 = false ; let result1 = !var1; console.log(result1); let val2 = true ; let result2 = !val2; console.log(result2); |
true false
JavaScript Bitwise NOT (~) Operator
The bitwise NOT (~) is a unary operator that inverts all the bits of its operand, converting each 0 to 1 and each 1 to 0.
Syntax:
a ~ b
Example: In this example, the variable num is initially set to 10, which is represented in binary as 00001010. When the bitwise NOT operator is applied to num, it inverts all the bits, resulting in 11110101. In the two’s complement representation used for signed integers, 11110101 represents -11.
Javascript
let num = 10; // Bitwise NOT, inverts all bits of 'num' let result = ~num; console.log(result); |
-11
JavaScript typeof Operator
The JavaScript typeof operator returns the data type of its operand in the form of a string. The operand can be any object, function, or variable.
Syntax:
typeof operand
Example: In this example, The typeof operator is used to check the data types of variables. Outputs: “number”, “string”, “boolean”, “object”, and “undefined” for each respective variable.
Javascript
let num = 18; let str = "neveropen" ; let isTrue = true ; let obj = { name: "Aman" , age: 21 }; let undefinedVar; console.log( typeof num); console.log( typeof str); console.log( typeof isTrue); console.log( typeof obj); console.log( typeof undefinedVar); |
number string boolean object undefined
JavaScript delete Operator
The delete operator in JavaScript removes a property from an object. If no other references exist, the property’s memory is automatically released.
Syntax:
delete object
// or
delete object.property
// or
delete object[ 'property' ]
Example: In this example, we are using the delete operator to delete the age property from our given object.
Javascript
let person = { name: "Ankit" , age: 21, city: "Noida" }; console.log(person); // Deleting the 'age' property // from the 'person' object delete person.age; console.log(person); |
{ name: 'Ankit', age: 21, city: 'Noida' } { name: 'Ankit', city: 'Noida' }
JavaScript void Operator
The void operator evaluates the given expression and then returns undefined.
Syntax:
void expression
void( expression )
Example: In this example, we are using the void operator.
Javascript
function myFunction() { return void 0; } console.log(myFunction()); |
undefined