Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unary operators "++" and "--" weird situation

Tags:

Here's a test situation for using the unary operator "++":

var j = 0 ; console.log(j); j = j++; console.log(j); 

For this, the output is:

0 0 

Since the ++ operator's position is at the back of the operand, so its precedence is lower than the assignment's precedence, I would expect "j" to first receive the value of itself (i.e.0), but then be incremented. So why is the second console.log(j) call still showing "0"?

Just to be clear, I know that the solutions are:

// 1) j++; // 2) ++j; // 3) j += 1; // 4) j = ++j; 

But I need to know why the increment step is not conducted in this specific scenario, NOT how to fix it!

like image 215
Andrei Oniga Avatar asked Jan 20 '14 13:01

Andrei Oniga


People also ask

What are unary operators explain with an example?

In mathematics, an unary operation is an operation with only one operand, i.e. a single input. This is in contrast to binary operations, which use two operands. An example is any function f : A → A, where A is a set.

Which is the correct example of an unary operator?

Which is the correct example of a unary operator? Explanation: &, == and / requires two operands whereas — requires only one operand, in general, it decreases the value of operand by 1.

Is a unary operator True or false?

Explanation: Unary operator as the name suggests is used to process the only one operand.

Which is a unique unary operation?

The select, project, and rename operations are called unary operations, because they operate on one relation.


2 Answers

This is an unintuitive (but not "weird"!) behaviour when using post-increment.

The statement j = j++ does this:

  1. Evaluate the LHS
    • In this case, nothing special happens because you simply named a variable, but this may not always be the case, e.g. foo() = j++
  2. Evaluate the RHS
    • Take the current value of j (0), and remember it as the result;
    • Increment j (to get 1);
  3. Assign the RHS to the LHS
    • recall that the RHS evaluates to that "remembered" value of j (0).

The result is a no-op.

The key here is that the entire RHS is evaluated and the post-increment performed, before the final assignment.


http://www.ecma-international.org/ecma-262/5.1/#sec-11.3.1
http://www.ecma-international.org/ecma-262/5.1/#sec-11.13.1

like image 60
Lightness Races in Orbit Avatar answered Oct 06 '22 15:10

Lightness Races in Orbit


According to the ECMA Specifications for Postfix Increment Operator,

  1. Let lhs be the result of evaluating LeftHandSideExpression.
  2. Throw a SyntaxError exception if the following conditions are all true:
    1. Type(lhs) is Reference is true
    2. IsStrictReference(lhs) is true
    3. Type(GetBase(lhs)) is Environment Record
    4. GetReferencedName(lhs) is either "eval" or "arguments"
  3. Let oldValue be ToNumber(GetValue(lhs)).
  4. Let newValue be the result of adding the value 1 to oldValue, using the same rules as for the + operator (see 11.6.3).
  5. Call PutValue(lhs, newValue).
  6. Return oldValue.

So, it is clear that the new value is first set on the lhs (in this case j) and the the old value is returned as the result of the expression, which is again set back in j. So, the value doesn't change.

like image 42
thefourtheye Avatar answered Oct 06 '22 14:10

thefourtheye