JavaScript Unary Operators
JavaScript Unary Operators work on a single operand and perform various operations, like incrementing/decrementing, evaluating data type, negation of a value, etc.
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).
let s1 = "12";
// Using unary plus to convert string to number
let x = +s1;
console.log(x);
// Here we are using typeof operator
console.log(typeof (x))
// "Geeks" cannot be converted to a number
let s2 = +"Geeks";
console.log(s2);
Output
12 number NaN
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.
let s1 = "12";
// Unary negation, negates the
// value of number
let x = -s1;
console.log(x);
// Unary negation, tries to convert
// 'Geeks' to a number
let s2 = -"Geeks";
console.log(s2);
Output
-12 NaN
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.
// Case 1: Postfix
let x = 12;
let y = x++;
console.log(x);
console.log(y);
// Case 2: Preifix
x = 10;
y = ++x;
console.log(x);
console.log(y);
Output
13 12 11 11
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.
let x = 8;
let y = x--;
console.log(x);
console.log(y);
x = 15;
y = --x;
console.log(x);
console.log(y);
Output
7 8 14 14
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.
let x = false;
let y = !x;
console.log(y);
x = true;
y = !x;
console.log(y);
Output
true false
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.
let x = 10;
// Bitwise NOT, inverts all bits of 'num'
let y = ~x;
console.log(y);
Output
-11
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.
let x = 18;
let s = "GeeksforGeeks";
let isTrue = true;
let obj = { name: "Aman", age: 21 };
let undefinedVar;
console.log(typeof x);
console.log(typeof s);
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.
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.
function myFunction() {
return void 0;
}
console.log(myFunction());
Output
undefined
JavaScript Unary Operators – FAQs
How does the unary plus (+) operator work?
The unary plus operator attempts to convert its operand to a number. If the operand is already a number, it returns the operand without changes.
How does the unary negation (-) operator work?
The unary negation operator converts its operand to a number and then negates it, changing its sign.
How does the typeof operator work?
The typeof operator returns a string indicating the type of the operand. The possible return values include “undefined”, “object”, “boolean”, “number”, “string”, “function”, and “symbol”.
How does the void operator work?
The void operator evaluates an expression and returns undefined. It is often used to ensure that an expression has no return value.
How does the delete operator work?
The delete operator removes a property from an object. It returns true if the property was successfully deleted, and false otherwise. It does not affect variables or function declarations.


