Unary operator in JavaScript
Unary operator in JavaScript
I'm happy to respond in English!
Unary operators in JavaScript are a type of operator that operates on a single operand. Unlike binary operators, which operate on two operands, unary operators work with just one value.
Here's the list of unary operators in JavaScript:
+
(Unary plus): This operator is used to convert a negative number to a positive number or to get the opposite sign of a number. Example: var x = -10; var y = +x; console.log(y); // outputs 10
-
(Unary minus): Similar to the unary plus, this operator converts a positive number to a negative one or gets the opposite sign of a number. Example: var x = 10; var y = -x; console.log(y); // outputs -10
!
(Logical NOT): This operator inverts the boolean value of its operand. If the value is true, it becomes false, and vice versa. Example: var x = true; var y = !x; console.log(y); // outputs false
~
(Bitwise NOT): This operator performs a bitwise NOT operation on its operand, which inverts all the bits of the number. Example: var x = 10; var y = ~x; console.log(y); // outputs -11
void
(Void operator): This operator is used to discard the return value of a function or expression.
Some important things to note about unary operators:
Order of Operations: Unary operators have a higher precedence than binary operators. When multiple operations are performed on an operand, the order of operations follows this sequence: unary operators first, then binary operators. Associativity: Most unary operators in JavaScript associate from left to right, except for thevoid
operator, which associates from right to left.
Here's an example demonstrating the associativity of unary operators:
var x = 10;
var y = -(-x); // outputs 10
console.log(y);
In this example, the !
and -
operators associate from left to right. First, the -
operator is evaluated, converting the number to its opposite sign (i.e., -10
). Then, the !
operator inverts the boolean value of the result (true
becomes false
, and vice versa), so the overall result is 10
.
In conclusion, unary operators are an essential part of JavaScript programming. They allow developers to perform single operations on a single operand, making code more readable and efficient. By understanding how these operators work and their order of operations, you can write better and more effective code.
Was this helpful? Let me know if you have any other questions!
Unary operator in java with example
I'd be happy to explain unary operators in Java and provide examples.
Unary operators in Java are operators that operate on a single operand. They are used to perform various operations such as increment, decrement, negation, bitwise inversion, and boolean negation.
Here are some common unary operators in Java:
++ (Increment): This operator increments the value of its operand by 1.Example: int x = 5; int y = ++x;
In this example, x
is incremented to 6 before it's assigned to y
. As a result, both x
and y
will be equal to 6.
Example: int x = 5; int y = --x;
In this example, x
is decremented to 4 before it's assigned to y
. As a result, both x
and y
will be equal to 4.
Example: int x = 5; int y = ~x;
In this example, the binary representation of x
is inverted. So if x
was 00000101 in binary, it will become 11111010.
Example: int x = 5; int y = +x;
In this example, the value of x
is simply assigned to y
. There's no change in the value.
Example: int x = 5; int y = -x;
In this example, the positive integer 5 becomes the negative integer -5.
Example: boolean x = true; boolean y = !x;
In this example, the logical negation of the boolean value true
is calculated, which results in false
.
These are some common unary operators in Java. They can be used to simplify code and improve readability.
Note that the post-increment and post-decrement operators (++x
and --x
) are also supported in Java, where the increment or decrement operation is performed after the expression has been evaluated.
In addition to these basic unary operators, Java also supports some more complex ones:
~(Bitwise AND with all bits cleared): This operator returns a new integer that is the bitwise AND of its operand and 0.Example: int x = 5; int y = ~x & 0;
In this example, the binary representation of x
is ANDed with 0. As a result, the output will be 00000000.
Example: int x = 5; int y = ~x | -1;
In this example, the binary representation of x
is ORed with -1. As a result, the output will be 11111111.
These are some common unary operators in Java. They can be used to simplify code and improve readability.