Yogesh Chauhan's Blog

# Unary Operators in JavaScript

in JavaScript on February 27, 2020

A unary operation is an operation with only one operand. Here are the examples.

#### delete

The delete operator deletes an object, an object's property, or an element at a specified index in an array. If the delete operator succeeds, it sets the property or element to undefined. The delete operator returns true if the operation is possible; it returns false if the operation is not possible.

``````
For e.g. delete myobj.h //returns true, delete array[3] //returns true``````

#### typeof

The typeof operator returns the type of the operand.

``````
For e.g. if var size = 1 then typeof size; // returns "number" and typeof true; // returns "boolean"``````

#### Unary plus

The unary plus operator precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already.

``````
For e.g. +'3' // output 3 or +true // output 1``````

#### Unary Minus

It does the same thing as Unary Plus and negates it as well.

``````
For e.g. var x = 3;
y = -x; // y = -3, x = 3
var x = "4";
y = -x; // y = -4``````

#### Logical Not

The logical NOT (!) operator performs logical negation on an expression.

``````
For e.g. !true === false and !false === true``````

#### Double Logical Not

It is possible to use a couple of NOT operators in series to explicitly force the conversion of any value to the corresponding boolean primitive.

``````
For e.g. n1 = !!true // !!truthy returns true``````

#### Increment Operator

The increment operator increments (adds one to) its operand and returns a value.

``````
For e.g. if var x = 3; then x++ will make it's value 4.``````

#### Decrement Operator

The decrement operator decrements (subtracts one from) its operand and returns a value.

``````
For e.g. if var x = 3; then x-- will make it's value 2.
``````