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.

Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to update database configurations in WordPress?WordPressHow to create a dynamic countdown using HTML and JavaScript?HTMLKanban vs Scrum: The two frameworks of agile principlesMiscellaneousHow to render Lists in React?ReactCheck If a String Contains a Specific Word in PHPPHPWhen you don’t want to @forward every member in SCSS (Sass)SCSS