JavaScript Unary Operators
Last Updated :
27 Feb, 2024
JavaScript Unary Operators work on a single operand and perform various operations, like incrementing/decrementing, evaluating data type, negation of a value, etc.
We will explore all the above operators along with their basic implementation with the help of examples.
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" ;
let num = +str1;
console.log(num);
console.log( typeof (num))
let str2 = + "Geeks" ;
console.log(str2);
|
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" ;
let num = -str1;
console.log(num);
let str2 = - "Geeks" ;
console.log(str2);
|
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
let num = 12;
let numPostfix = num++;
console.log(numPostfix);
console.log(num);
let num1 = 10;
let num1Prefix = ++num1;
console.log(num1Prefix);
console.log(num1);
|
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;
let num1Postfix = num1--;
console.log(num1Postfix);
console.log(num1);
let num2 = 15;
let num2Prefix = --num2;
console.log(num2Prefix);
console.log(num2);
|
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);
|
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;
let result = ~num;
console.log(result);
|
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 = "GeeksforGeeks" ;
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);
|
Output
number
string
boolean
object
undefined
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);
delete person.age;
console.log(person);
|
Output
{ name: 'Ankit', age: 21, city: 'Noida' }
{ name: 'Ankit', city: 'Noida' }
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());
|
Share your thoughts in the comments
Please Login to comment...