The Wayback Machine - https://web.archive.org/web/20240814094032/https://www.geeksforgeeks.org/javascript-unary-operators/
Open In App

JavaScript Unary Operators

Last Updated : 29 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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";

// 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);

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.

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);

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.

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);

Output
12
13
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.

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);

Output
8
7
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.

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);

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.

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); 

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.

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);

// Deleting the 'age' property 
// from the 'person' object
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());

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.



Previous Article
Next Article

Similar Reads

JavaScript Arithmetic Unary Plus(+) Operator
The Unary plus(+) operation is a single operand operator (which means it worked with only a single operand preceding or succeeding to it), which is used to convert its operand to a number, if it isn't already a number. Syntax: +Operand Below examples illustrate the Unary plus(+) Operator in JavaScript: Example 1:This example shows the use of the Ja
1 min read
JavaScript Arithmetic Unary Negation(-) Operator
The Unary negation(-) operation is a single operand operator (which means it worked with only a single operand preceding or succeeding to it), which is used to convert its operand to a negative number, if it isn't already a negative number. Syntax: -Operand Example 1: This example shows the use of JavaScript Unary negation(-) Operator. C/C++ Code
1 min read
What is a Unary Function in JavaScript?
A unary function is similar to the unary operator as the unary operator allows you to operate it with only one operand in a similar way a unary function allows you to pass only a single parameter to it. A unary function accepts only a single parameter and operates the different operations on it inside the function body. A unary function can be defi
1 min read
Underscore.js _.unary() Method
The Underscore.js _.unary() method returns a new function that accepts only one argument and passes this argument to the given function. Additional arguments are discarded. Syntax: _.unary( fun ) Parameters: This method takes a single parameter as listed above and discussed below: fun: This is the given function passed as parameter. Return Value: T
2 min read
JavaScript Course Logical Operators in JavaScript
logical operator is mostly used to make decisions based on conditions specified for the statements. It can also be used to manipulate a boolean or set termination conditions for loops. There are three types of logical operators in Javascript: !(NOT): Converts operator to boolean and returns flipped value &&:(AND) Evaluates operands and retu
3 min read
JavaScript Course Operators in JavaScript
An operator is capable of manipulating a certain value or operand. Operators are used to performing specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. In JavaScript, operators are used for comparing values, performing arithmetic operations, etc. There are various operators
7 min read
Alternative to Multiple OR Operators in JavaScript
In this article, we will learn the alternatives to the use of multiple || (OR operator) in conditional statements. Multiple OR operators in conditional statements might cause poor readability of code and are often limited in functionality. Example: The below JavaScript code could be rewritten to perform the same task in other ways: C/C++ Code let u
4 min read
How to use multiple ternary operators in a single statement in JavaScript ?
In this article, we will see how we can use multiple ternary operators in a single statement in JavaScript. Sometimes using conditional statements like if, else, and else if stretches the code unnecessarily and makes the code even more lengthy. One trick to avoid them can be the use of ternary operators. But what if we are to use multiple if-else s
2 min read
Javascript Short Circuiting Operators
In JavaScript short-circuiting, an expression is evaluated from left to right until it is confirmed that the result of the remaining conditions is not going to affect the already evaluated result. If the result is clear even before the complete evaluation of the expression, it short circuits and the result will be returned. Short circuit evaluation
3 min read
What are Comparison Operators in JavaScript ?
Comparison operators in JavaScript are used to compare two values and return a boolean result (true or false). They include operators such as ==, ===, !=, !==, >, <, >=, and <=. Example: Here, the > (greater than) the operator compares the values of a and b. Since a is not greater than b, the expression a > b evaluates to false, w
1 min read
three90RightbarBannerImg