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 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to add multiple Columns in CSS like Bootstrap?CSSHow different is Handling Events in React vs HTML DOM?ReactOverview of Drupal and how it worksDrupalConfiguring Modules with @forward rule in SCSS (Sass)SCSSIntroduction to Angular modules Part 1: NgModule metadataAngularHow to Use the EXISTS and NOT EXISTS Operator with a Subquery in SQL and MySQL?SQL/MySQL