YogeshChauhan . com

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.
amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

first-of-type and last-of-type selectors in CSSCSSIntroduction to Angular modules Part 2: NgModules and componentsAngularHow to select multiple values in React dropdown?React3 ways to pass a variable in url() function in SCSS (Sass)SCSSWordPress: How to print ACF repeater field values?WordPressMIN, MAX, COUNT, AVG and SUM in SQLSQL/MySQL