I have a simply array and counter:
var data = [1];
var i = 0;
The shortcut assignment produces 2:
data[i++] *= 2
// 2
I was expecting 3. data[i++] is multipled with 2, so 1 * 2 is 2, and then that is assigned to data[i++], which now becomes 2, and then after statement evaluated the side effect of ++ causes i to be 3.
The following also gives me unexpected result. It produces NaN oddly enough.
var data [1];
var i = 0;
data[i++] = data[i++] * 2;
// NaN
I was expecting 3 again. data[i++] first evaluates to 1, and then is multipled with 2, and then that 2 value is assigned to i in data[i++], which is then incremented after statement completes, causing it to be 3.
What am I missing here?
var data [1]; is not a valid JavaScript. Did you mean var data = [1];?
data[i++] *= 2 is evaluated as follows:
i++, as the innermost expression resolves first: its value is i (i.e. 0), and i increments afterwards to 1.
data[0] is looked up, and multiplied by two; since data[0] is 1, data[0] gets assigned the value of 1 * 2, i.e. 2.
The value of the outermost expression is returned: 2. ++ increments only what it was applied to (i), and not the whole expression.
data[i++] = data[i++] * 2 evaluates as follows:
The first i++ evaluates to 0 and modifies i to 1, as before.
The second i++ evaluates to 1 and modifies i to 2
The expression then evaluates as data[0] = data[1] * 2. data[1] is undefined, and undefined * 2 is not a number (NaN).
In general, it is strongly recommended to avoid having two increment/decrement operators in the same expression. Different languages (and indeed, different compilers of the same language) have wildly different ideas of what should be done. In many languages, it is declared "undefined behaviour" in language specification.
Instead of i++, use ++i. In your case, you're first returning i, then incrementing it, while you're looking for an increment, and return it after.
Related
I'm just curious about one thing. A little example in Javascript
var a = 1;
a = a++;
console.log(a); // 1
var b = 1;
b = ++b;
console.log(b); // 2
var c = 1;
c += 1;
console.log(c); //2
I understand why it works this way in case b and c, but what about a?
At first the code makes an assignment a = a, the value stays the same actually, but then it should (as I see) make increment and increase value a per unit. But this not happening. Why?
var a = 1;
a = a++;
1 is assigned to a
a++ is evaluated as 1
a++ increments a to 2
a = {result of previous evaluation} assigns the 1 to a so it is 1 again
var b = 1;
b = ++b;
1 is assigned to b
++b increments b to 2
++b is evaluated as 2
b = {result of previous evaluation} assigns the 2 to b so it is 2 still
That is how post increment works
If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing.
a = a++;
when a++ executes a is 2 and the expression returns 1 and a gets assigned to 1.
That is the reason you seeing the value before increment.
Note that if you don't assign back, you see the incremented value of a. You in short ovverriding the incremented value here by assigning it back.
var a = 1;
a++;
console.log(a); // 1
It is rumored that to execute ++a, the value in memory increases and then returns. While for a++, first the value is stored in the created temporary variable, then the value of the primary variable is incremented and the value of the temporary variable is returned - thus the "expenses" for creating the temporary variable increase.
In the case of a++ the return value is actually the original value. You’ve assigned the original value to a again.
The a++ returns the value that before self-increase;
While the ++a returns the value that after the self-increase;
so this is why when you call a = a++, a is equal to 1;
a= a++ return the previous value of a before increment it's value.
you can just use a++
The postfix operator returns the original value of a.
So a is increased by the postfix ++ operator but then a is overwritten by assigning the return value of the operator, which is the value before incrementing.
The return value of a of 1 is temporary stored for assingment, then the increment takes place, a has now the value of 2 and then the assingmet of the former stored value happens.
a = a++;
is the same like
a = (temp = a, a++, temp);
var a = 1;
// Here a++ means a = a; then a = a + 1
a = a++; // so you are assign first step value inside a so thats way a = 1
console.log(a); // 1
means you are storing value that time when a++ is equal to a = a. You simply assigning the value of a and replacing the value.
This question already has answers here:
javascript i++ vs ++i [duplicate]
(8 answers)
Closed 10 months ago.
let a = 70;
a++; // This returns the value before incrementing.
console.log(a) // returns 71
Can someone explain to me why this doesn't return 70, please?
let b = 70;
++b; // Using it as a prefix returns the value after incrementing.
console.log(b) // returns 71
I understand how prefix works.
I have read explanations and watched videos on this subject but it's still confusing to me. Thank You.
Both versions of them increment the value they're applied to. If you do nothing with the value of the increment expression itself, they're equivalent, both of them doing the same thing as i += 1. The only time it matters is when you use the result of the increment expression:
let a = 70;
console.log(a++); // outputs 70, the old value of a, but a will be 71 afterwards
console.log(a) // outputs 71; no matter how you increment, after the increment, the value has changed
let b = 70;
console.log(++b); // outputs 71; the new value of b is produced
console.log(b) // outputs 71; same as in case a
The difference you are trying to notice is only visible when you use these prefix/postfix operators inline, something like this:
let a = 5;
console.log(a++);
let b = 5;
console.log(++b);
"return" isn't the right term (functions return, expressions do not).
a++ evaluates as the value before incrementing.
You aren't looking at the value the expression evaluates as, you are just looking at the value of the variable after the increment.
let a = 70;
let result_of_a_plus_plus = a++; // This evaluates as the value before incrementing.
let b = 70;
let result_of_plus_plus_b = ++b; // Using it as a prefix increments the value before the expression is evaluated.
console.log({
a,
result_of_a_plus_plus,
b,
result_of_plus_plus_b
});
Here's the code:
var n = 4;
while(n) {
console.log("Hey!");
n = 5;
}
Am I right here:
n is a variable, with value 4.
while n = 4, print "Hey" in console.
change value of n to 5.
check if n = 4.
n is not equal to 4, so stop executing the code!
Where am I wrong? I'm really frustrated! Edited it a million times! And the worst thing, browser crashes every time!
Please correct me!
Am I right here:
n is a variable, with value 4.
while n = 4, print "Hey" in console.
while(n) doesn't check to see if n is 4, it checks to see if n is truthy*. Both 4 and 5 are truthy values.
To check if it's 4, use == or ===:
while (n === 4) {
console.log("Hey!");
n = 5;
}
(For more about == vs. ===, see this question and its answers.)
* JavaScript uses type coercion in many places, including when testing conditions for while loops and similar. So we talk about values being "truthy" (they coerce to true) or "falsey" (they coerce to false). Truthy values are all values that aren't falsey. The falsey values are 0, "", null, undefined, NaN, and of course, false.
Astonished to find out that a line like this :
$('#TextBox').val(parseInt($('#TextBox').val())++ );
Will not work !
I've done some tests, it plays out to the conclusion that the inline ++ doesn't work (in Javascript as a whole ? Or simply in my example ?).
Here's a test with three pieces of code, it seems that ++ is ok with a variable but not inline.
So, no inline ++ for us in Javascript?
There's nothing particular to jQuery about this. ++ increments a variable. You are trying to increment the return value of a function call.
Q: What does x++ mean?
A: x++ means take the value of x, let's call this n, then set x to be n + 1, then return n.
Q: Why does this fail on a non-variable?
A: Let's try it on something simple, say 3, and see where things go wrong.
Take the value of 3 and call it n, okay, n = 3
Set 3 to be n + 1, so 3 = 3 + 1, 3 = 4 this makes no sense! So if this step can't be done, the ++ operator can't be used.
++ works on variables, not directly on numbers
var c = parseInt($('#TextBox').val());
$('#TextBox').val( ++c );
Change the order from
var x = 0;
var result = x++;
result // 0
To
var x = 0;
var result = ++x;
result // 1
Then it will evaluate ++ before retrieving the value.
This question already has answers here:
How does [b][b = a,0] swap between a and b?
(2 answers)
Closed 9 years ago.
I do not know how to learn demo 2,because it's difficult for me.
//demo1.js
var a = 1;
var b = 2;
var c;
c = b;
b = a;
a = c;
log(a); // a = 2
log(b); // b = 1 I can read this one.
//demo 2.js
var a = 1, b = 2;
a = [b][b = a, 0]; // why? '0' is a varible?
console.log(a,b) //output a = 2, b =1
// v---- 1. v---- 3.
a = [b][b = a, 0];
// ^---^-- 2.
Put the value of the b variable in a new Array.
The next set of square brackets is the member operator used for getting the index. In this case, that space is being used to reassign the value of the a variable to the b variable. This can be done because the original value of b is safely in the Array (from step 1.).
Separated by comma operator, the 0 index is then used as the actual value of the Array being retrieved, which if you'll recall is the original b value. This is then assigned to a via the first = assignment on the line.
So to summarize, b is put in the Array, and is retrieved via the 0 index and assigned to a on the left, but not before a is assigned to b (borrowing the space of the [] member operator).
This could also be written like this:
a = [b, b = a][0];
Only difference now is that the second index of the Array is used to do the assignment. Probably a little clearer like this.
The comma operator in Javascript evaluates its left operand, throws it away and returns its right operand after evaluating. Its only use is when the left operand has a side-effect (like modifying a variable's value) but you don't want its result.
[b] defines an array containing b.
[b = a, 0] evaluates b = a - so b now contains the value of a. Then it throws it away. Then it takes the element at index 0 of [b] - returning b. a now contains the value of b that was stored there, rather than the up-to-date value of b, so our swap is successful (albeit obfuscated).