Javatpoint Logo
Javatpoint Logo

JavaScript unary operators

Unary operators in JavaScript are unique operators that consider a single input and carry out all possible operations.

The Unary plus, unary minus, prefix increments, postfix increments, postfix decrements, and prefix decrements are examples of these operators. These operators are either put before or after the operand.

The unary operators are more effective in executing functions than JavaScript; they are more popular. Unary operators are flexible and versatile since they cannot be overridden.

Types of unary operators

One value is the focus of unary operators. The unary functions and their descriptions are listed in the following table:

Unary Operators Operator's Name Operators Description
+x Unary Plus The operator converts an input value into a number
-x Unary Minus The operator converts a value into a number and negates it
++x Increment Operator (Prefix) The operator uses to inserts one value before the incremental value by one
--x Decrement Operator (Prefix) The operator Subtracts one value from the given input value before
x++ Increment Operator (Postfix) The operator uses to inserts one value after the incremental value by one
x-- Decrement Operator (Postfix) The operator subtracts one value before the incremental value by one.

Unary operator's description

The multiple operators use javascript to show the definition, syntax, example, and description.

Unary plus (+) operator

The simple plus symbol (+) is the unary plus operator. The unary addition has no effect when put in front of a number.

Syntax

The following unary plus operator shows the functionality with the value.

The following table outlines the rules used by the Number() function when the unary plus operator is applied to a non-numeric value:

Value Result
boolean The output shows 0 to false and 1 to true
string It converts the input string value based on particular rules
object The Object work with the valueOf() or toString() method to get the required value. The value converts into a number.

Examples

The following examples use to addition operator using the variable operand and unary plus operator.

Example 1: the given example shows the basic operation of the unary plus (+) operator with the operand. Here, we use a simple plus sign with the numeric data.

Output

The unary plus operator data is shown as an output.

JavaScript unary operators

Example 2: the following example shows the basic operation of the unary plus (+) operator with the operand. Here, we need a function with the "this" keyword for the basic value.

Output

The unary plus operator data is shown as an output.

JavaScript unary operators

Unary minus (-) operator

A single minus sign (-) is the unary minus operator. A number is negated when the unary minus operator is used on it.

Syntax

The following unary minus operator shows the functionality with the value.

The unary minus operator cancels a value when used to a non-numeric value after converting it to a number according to the same guidelines as the unary plus operator.

Examples

The following examples use to subtraction operator using the variable operand and the unary minus operator.

Example 1: the given example shows the simple operation of the unary plus (-) operator with the operand.

Output

The unary minus operator shows as output data.

JavaScript unary operators

Example 2: the following example shows the basic operation of the unary minus (-) operator with the operand. Here, we need a function with the "this" keyword for the basic value.

Output

The unary minus operator data shows as an output.

JavaScript unary operators

Unary Logical Not(!) operator

Unary logical not operator works the logical operation and Boolean output. It converts the value in the negative data and shows the Boolean value as an output.

Example

The given an example displays the basic operation of the unary Logical Not(!) operator

With operand. Here, we need a function with the not operator and the input data.

Output

The unary logical, not operator, data is shown as an output.

JavaScript unary operators

Prefix Unary increment (++) operator

Javascript unary increment operator increments the value before operation. This operator increments the value by one of the original and default values. This operator needs an operand to add the value and return the additional value.

Syntax

The following unary increment operator shows the functionality with the value.

The unary increment operator cancels a value when used to a non-numeric value. It works on the numerical input values for the addition operation.

Examples

The following examples use addition by one (increment) operator using the variable operand and unary increment operator.

Example 1: The given example displays the basic operation of the unary increment (++) operator with the operand.

Output

The unary incremental operator data shows as an output.

JavaScript unary operators

Example 2: The given example displays the basic operation of the unary increment (++) operator with the operand. Here, we need a function with the "this" keyword for the basic addition operation and its output value.

Output

The unary incremental operator data shows as an output.

JavaScript unary operators

Prefix Unary decrement (--) operator

Javascript unary decrement operator is used to decrement the value before operation. This operator decrements the value by one of the original and default values. This operator needs an operand to add the value and return the additional value.

Syntax

The following unary decrement operator shows the functionality with the value.

Example

The given example displays the basic operation of the unary decrement (--) operator with the operand.

Output

The unary decrement operator data shows as an output.

JavaScript unary operators

Postfix Unary increment (++) operator

Javascript unary increment operator increments the value after the operation. This operator is used to increment value by one of the default values after the mathematical operation.

Syntax

The following postfix unary increment operator with the value.

The unary increment operator cancels a value when used to a non-numeric value. The original variable can be incremented by one.

Example

The given example displays the operation of the postfix unary increment (++) operator with the operand.

Output

The unary incremental operator data shows as an output.

JavaScript unary operators

Postfix Unary decrement (--) operator

Javascript unary decrement operator decrements the value after the operation. The operator works to decrement the value by one of the default values after the mathematical operation.

Syntax

The following postfix unary decrement operator with the value.

Example

The given example displays the operation of the postfix unary decrement (--) operator with the operand.

Output

The postfix unary decremental operator data shows as an output.

JavaScript unary operators

Unary bitwise not (~) operator

It is a logical operator to operate binary, not an operation using invert functionality. It works for inverting the input value of the script.

Example

The given example displays the operation of the unary bitwise not (~) operator with the operand.

Output

The unary bitwise not operator data shows as an output.

JavaScript unary operators

Unary delete operator

The deletes operator is a unary operator in javascript to delete the specific index of the array. It does not affect the operand, variable, and other functionality. The operator uses to delete a specific index before the operand with a variable and shows values as an output.

Example

The given example displays the operation of the unary delete operator with the operand.

Output

The unary delete operator and show operand as an output.

JavaScript unary operators

Unary void operator

The void operator is a unary operator but uses as an expression to return the output of the undefined input data. The empty return type shows the function's information in the output tab. The void operator contains no input data and shows an undefined value.

Example

The given example displays the operation of the unary void operator with the javascript function.

Output

The unary void operator and show operand as an output.

JavaScript unary operators

Conclusion

The unary operators help to use different operations and functionality simply. It is easy for developers and users to execute javascript values.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA