Unary Operator in JavaScript

Unary Operator in JavaScript | An operation in mathematics is a computation on one or more values known as operands that results in an output value. An operator is a sign or symbol that links input values with operands. A single-operand operation is referred to as a unary operation. Either before or after the operator is this operand.

Compared to conventional JavaScript function calls, unary operators are more effective. Unary operators can also not be overridden, therefore their functionality is assured.

OperatorDescription
Unary plus (+)Tries to convert the operand into a number.
Unary negation ()Tries to convert the operand into a number and negates after.
Increment (++)Adds one to its operand.
Decrement (--)Decrements by one from its operand.
Logical NOT (!)Converts to Boolean value then negate it.
Bitwise NOT (~)Inverts all the bits in the operand and returns a number.
typeofReturns a string which is the type of the operand.
deleteDeletes specific indexes of an array or specific properties of an object.
voidDiscards a return value of an expression.

Unary plus (+) Operator in JavaScript

The simple plus symbol (+) is the unary plus operator. The unary addition has no effect when put in front of a number. For instance:-

let myValue = '978';
+myValue; // 978
let isMember = true;
+isMember; // 1
let name = 'riz';
+name; // NaN

Unary Negation (-) Operator in JavaScript

Similar to the unary plus, the unary negation (-) first turns the value into a number before negating it.

let balance = '20.55';
-balance; // -20.55;
+true; // 1
+false; // 0
+'73'; // 73
+null; // 0
+undefined; // NaN
+'hello'; // NaN

Increment(++) Operator in JavaScript

The value is increased by 1 and returned via the increment operator. You can use the “++” operator either in prefix style or in postfix style. The operator (++) occurs before the operand (any value) in a prefix increment, but it comes after the operand (any value) in a postfix increment:-

let x = 10;
console.log("Value of x: ", x);
let y = ++x;
console.log("Value of x: ", y);

let a = 100;
console.log("Value of a: ", a);

let b = a++;
console.log("Value of a: ", a);
console.log("Value of b: ", b);

Output:-

Value of x: 10
Value of x: 11
Value of a: 100
Value of a: 101
Value of b: 100

Decrement(–) Operator in JavaScript

The Decrement operator “–” subtracts one from the operand’s value when applied to a value. There are two ways to use the “–” operator: prefix style and postfix style.

let x = 10;
console.log("Value of x: ", x);
let y = --x;
console.log("Value of x: ", y);

let a = 100;
console.log("Value of a: ", a);

let b = a--;
console.log("Value of a: ", a);
console.log("Value of b: ", b);

Output:-

Value of x: 10
Value of x: 9
Value of a: 100
Value of a: 99
Value of b: 100

Logical Not (!) Operator in JavaScript

Another unary operator not represented by “!” is called logical not in JavaScript. It is logical to avoid inverting true and false values:-

let x = true;
let y = false;
console.log("Value of x is equal to value of y: " , (x = !y));

Output:-

Value of x is equal to value of y: true

Any computer language relies heavily on unary operators to function. They focus on a single operand and, depending on the operator, carry out various operations on it. In this tutorial, we discuss a few important unary operators and provide examples for each. Unary +, unary -, increment, decrement, and logical operators have all been demonstrated, along with examples of how to use each one in JavaScript.

Leave a Comment

Your email address will not be published. Required fields are marked *