Faster, better AI-powered code reviews. Start your free trial!  
Faster, better AI-powered code reviews.
Start your free trial!

Get high quality AI code reviews

Javascript Unary Operators: Javascript Explained

Table of Contents

Unary operators are a type of operation that only requires one operand, or input value, instead of the two typically used by binary operators. In Javascript, unary operators are used to perform various operations such as incrementing or decrementing variables, as well as deleting elements from an array. This article will explore the various Javascript unary operators, explain their purposes, and discuss how they are used in coding.

What are Unary Operators?

In Javascript, unary operators are used to manipulate data and variables. Unary operator perform an operation on one operand (input value) and are generally placed before the operand. For example, the unary operator “!” is a logical not operator, that inverts the value of a given expression. In the line of code “let y = !x;”, the unary operator “!” is placed before the operand x. This would evaluate to evaluate to true if x is false, and false if x is true.

Unary operators can also be used to increment or decrement a variable. For example, the unary operator “++” is used to increment a variable by one. In the line of code “let x = y++;”, the unary operator “++” is placed after the operand y. This would evaluate to the value of y, and then increment y by one.

Common Unary Operators

The most common unary operator in Javascript are the increment (++) and decrement (–) operators. The increment operator adds one to a given value, while the decrement operator subtracts one. These two operations can be useful when looping through an array or incrementing a counter. Additionally, there are unary operators specific to changing the data type of a given element. These include unary + and – operators, which convert a value from string type to number type. For example, writing let z = +”30″; would assign the numeric value 30 to z.

Unary operators can also be used to perform logical operations. The logical NOT operator (!) is used to reverse the boolean value of a given expression. For example, writing let x = !true; would assign the boolean value false to x. Similarly, the logical NOT operator can be used to check if a given value is undefined or null. Writing let y = !x; would check if x is undefined or null, and assign the boolean value true to y if it is.

Using Unary Operators with Variables

Unary operators can be used in various ways when working with variables. For instance, the increment operator can be applied before and after a given variable to increase its value by two. When applied before the variable (prefix), the increased value is returned. When applied after the variable (postfix), the pre-incremented value is returned. For example: let x = 3; console.log(x++); would return 3 while console.log(++x); would return 4.

Increment and Decrement Operators

The increment and decrement operators in Javascript can be a valuable tool when working with code. They provide the ability to quickly and easily adjust a variable’s value by either incrementing or decrementing by one. Using these operators can simplify many operations, particularly looping and counting operations, by enabling a variable to track progress.

Increment and decrement operators can also be used to quickly adjust the value of a variable by a larger amount. For example, the increment operator can be used to add 5 to a variable’s value, while the decrement operator can be used to subtract 5 from a variable’s value. This can be useful when working with larger numbers or when needing to adjust a variable’s value by a specific amount.

Logical Not Operator

The logical Not operator (!), also known as the bang operator, is used to invert the value of an expression from true to false, or from false to true. This operation can be used to determine whether a certain piece of data is present in a variable. For example: let z = ‘hi’; if (!z) is the same as writing if (z == false). This code would evaluate as false because the variable z contains a string.

The logical Not operator can also be used to check if a variable is undefined. For example, if (!x) would evaluate to true if the variable x is undefined. This can be useful when checking if a variable has been declared before attempting to use it.

Bitwise Not Operator

The bitwise Not operator (~) is similar to the logical Not operator but it works with bitwise operators. This operator is used to invert each bit in a number, meaning it changes all 0’s to 1’s and 1’s to 0’s. It is commonly used when dealing with larger numbers because it simplifies operations over several bits.

The bitwise Not operator is also known as the one’s complement operator. It is a unary operator, meaning it only requires one operand. It is often used in combination with other bitwise operators to perform operations on binary numbers. For example, it can be used to invert the bits of a number, or to set a bit to 0 or 1.

Unary Plus and Minus Operators

Unary plus and minus operators are often used to change the data type of a given element, converting it from a string to a number for example. The plus operator (+) attempts to convert the operand it precedes into a number, while the minus operator (-) does the same but with the added benefit of being able to affect the magnitude of a number’s sign without affecting its value.

These operators are also used to increment or decrement a number by one. For example, the plus operator can be used to increment a number by one, while the minus operator can be used to decrement a number by one. This is often used in loops and other programming structures to iterate through a set of values.

Deleting Elements with the Delete Operator

The delete operator can be used to delete a key from an object or an element from an array. It must be followed by an element in brackets []. The delete operator does not actually remove the specified element from the array, nor does it affect its length. Instead, it leaves a placeholder in its place that evaluates to undefined.

When using the delete operator, it is important to note that it will not work on variables or functions. It is only applicable to objects and arrays. Additionally, the delete operator does not affect the order of the elements in the array, so the array will remain in the same order as before the element was deleted.

Summary of Javascript Unary Operators

Unary operators are an essential part of working with Javascript code. They provide essential functions such as incrementing and decrementing their operands or changing data types, as well as allowing for simpler code when performing operations such as looping and counting. It is important to understand the various unary operators available so they may be used in code effectively.

Sarang Sharma

Sarang Sharma

Sarang Sharma is Software Engineer at Bito with a robust background in distributed systems, chatbots, large language models (LLMs), and SaaS technologies. With over six years of experience, Sarang has demonstrated expertise as a lead software engineer and backend engineer, primarily focusing on software infrastructure and design. Before joining Bito, he significantly contributed to Engati, where he played a pivotal role in enhancing and developing advanced software solutions. His career began with foundational experiences as an intern, including a notable project at the Indian Institute of Technology, Delhi, to develop an assistive website for the visually challenged.

Written by developers for developers

This article was handcrafted with by the Bito team.

Latest posts

Mastering Python’s writelines() Function for Efficient File Writing | A Comprehensive Guide

Understanding the Difference Between == and === in JavaScript – A Comprehensive Guide

Compare Two Strings in JavaScript: A Detailed Guide for Efficient String Comparison

Exploring the Distinctions: == vs equals() in Java Programming

Understanding Matplotlib Inline in Python: A Comprehensive Guide for Visualizations

Top posts

Mastering Python’s writelines() Function for Efficient File Writing | A Comprehensive Guide

Understanding the Difference Between == and === in JavaScript – A Comprehensive Guide

Compare Two Strings in JavaScript: A Detailed Guide for Efficient String Comparison

Exploring the Distinctions: == vs equals() in Java Programming

Understanding Matplotlib Inline in Python: A Comprehensive Guide for Visualizations

Related Articles

Get Bito for IDE of your choice