Explain +Var and -Var Unary Operator in JavaScript

An example of a logical opposite value of a variable in javascript

You aren't assigning !a to any variable. a is still true.

What you want to do is this,

a = true;
a = !a;
document.writeln(a);

+ operator before expression in javascript: what does it do?

The unary + operator can be used to convert a value to a number in JavaScript. Underscore appears to be testing that the .length property is a number, otherwise it won't be equal to itself-converted-to-a-number.

lvalue required as increment operand - + unary operator

An lvalue is an expression that potentially designates an object (bytes in memory). The most common form of lvalue is simply a name: Given int x, float y, or char z[10], then x, y, and z are lvalues. Another form of lvalue is the result of unary *: Given double *p;, then *p is an lvalue.1

When we use lvalues to compute expressions, they are turned into the values of their objects: x+3 produces three more than whatever value is in x. In this expression x is used for its value. In contrast, in x = 7;, x is used directly as its lvalue; 7 is written to the bytes in memory, regardless of what value they previously contained.

Since the C standard must specify how compilers behave in technical details, it specifies that, when an lvalue is used in an expression in a place where we want its value, that lvalue is converted to a value. To emphasize that this result is just a number or similar thing and is no longer a reference to memory, it may be called an rvalue.

Unary + is an operator. When applied to an arithmetic value, it produces the same value. However, it only produces a value, not an lvalue. So applying + to an lvalue produces an rvalue.

Footnote

1 This is why an lvalue potentially designates an object: *p is an lvalue and is treated as such during compilation, but, when the program is executing, p might be set to a null pointer, so *p would not actually be an object. That would be an error in the program, but it is a run-time error.

Is there any reason not to use the plus operator instead of Number() or parseInt() to return a number?

As far as I know the first two are completely equivalent, and the choice between them is a matter of taste. (Personally I prefer the unary + because it's more concise, and well understood by most JS developers.)

parseInt is different because it reads a number value from the start of the string and ignores the rest when it reaches a non-numeric character. A common use is getting the underlying number from a CSS value like "20px". Note that the other two methods would fail with a NaN in this case.

what is the output of conditional operator with unary operator

The expression on the right hand side is evaluated like an if-statement.

if (Delay == true)
return 1;
else
return -1;

The result is then used for the += assignment.

In the
C++20 draft standard that's

7.6.19 (6) (Assignment and compound assignment operators)

The behavior of an expression of the form E1 op= E2 is equivalent to E1 = E1 op E2 except that E1 is
evaluated only once. [...]

Since Delay == false, the return value of the ternary operator is -1. The fact that you're operating on a boolean instead of an int can make it look like you got the +1 back.

Note that you get a compiler warning C4804:

warning C4804: '+=': unsafe use of type 'bool' in operation

Is it undefined behavior? No.

7.6.19 (6) (Assignment and compound assignment operators)

[...] For += and
-=, E1 shall either have arithmetic type or be a pointer to a possibly cv-qualified completely-defined object
type. In all other cases, E1 shall have arithmetic type.

and

7.3.8 (2) (Integral conversions)

If the destination type is bool, see 7.3.14.

which says

7.3.14 (1) (Boolean conversions)

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer-to-member type can be converted to a
prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false;
any other value is converted to true.

So the -1 is converted to true and true prints as 1.

Using ++ Unary Operator with a Map Get function in Java

Look at JLS §15.14.2:

A postfix expression followed by a ++ operator is a postfix increment expression.

PostIncrementExpression:
PostfixExpression ++

The result of the postfix expression must be a variable [...]

This clearly defines the syntax of the postfix increment expression, and also clearly states that the postfix expression must be a variable.

Thus, you simply cannot use the operator ++ onto a method call.

Operator precedence of unary operators

My programming ruby book (2nd edition) also lists unary operators as having higher precedence than assignment.

The unary operator IS being given highest precedence. The reason the line is parsed as ~ (a = 1) is because decomposing the line into valid syntax is of higher precedence than anything else, including using the simple variable 'a' as the expression the unary operator operates on.

If the ruby parser could have made something valid of the rest of the line, it would have used (~ a), but there is no valid rule than matches = something, only lvalue '=' rvalue.

You can regard "valid syntax" as the top priority, then simple values, constant and variable names and then the standard operators under that.



Related Topics



Leave a reply



Submit