This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
++someVariable Vs. someVariable++ in Javascript
I know you can add one to a variable simply by doing i++ (assuming i is your variable). This can best be seen when iterating through an array or using it in a "for" statement. After finding some code to use online, I noticed that the for statement used ++i (as apposed to i++).
I was wondering if there was any significant difference or if the two are even handled any differently.
Yes there is a big difference.
var i = 0;
var c = i++; //c = 0, i = 1
c = ++i; //c = 2, i = 2
//to make things more confusing:
c = ++c + c++; //c = 6
//but:
c = c++ + c++; //c = 13
And here is a fiddle to put it all together: http://jsfiddle.net/maniator/ZcKSF/
The value of ++i is i + 1 and the value of i++ is just i. After either has evaluated, i is i + 1. It's a difference in timing, which is why they're often called 'pre-increment' and 'post-increment'. In a for loop, it rarely matters, though.
People like Douglas Crockford advise not to use that way of incrementing, amongst other reasons because of what Rafe Kettler described. No matter how experienced you are, sometimes ++i/i++ will suprise you. The alternative is to simply add 1 to i using i += 1, readable, understandable and unambiguous.
have a look at this link : http://www.w3schools.com/js/js_operators.asp
it's post increment versus pre increment. They both end up incrementing the value but one returns the value BEFORE incrementing (++y) and the other one returns the value AFTER (y++).
However, it doesn't make any difference when using it in a for loop --
for( var i = 0; i < 100; i++ ) { ... }
is the same as
for( var i = 0; i < 100; ++i ) { ... }
a=1;
b=1;
c=++a;//the value of a is incremented first and then assigned to c
d=b++;//the value of b is assigned to d first then incremented
now if you print a,b,c,d..the output will be:
2 2 2 1
++i is called pre-increment and i++ is called post-increment. The difference is when the variable is incremented. Pre-incrementing a variable usually adds 1 and then uses that value, while post-incrementation uses the variable and then increments.
Related
This question already has answers here:
Difference between pre-increment and post-increment in a loop?
(22 answers)
Closed 1 year ago.
The output of console.log(in1()) should be 11 but it's 10 why?
function outer() {
let i = 10;
return function inner() {
return i++;
}
}
var in1 = outer();
console.log(in1());
Quoting https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment here:
If used postfix, with operator after operand (for example, x++), the increment operator increments and returns the value before incrementing.
Therefore, the postfix (that is, x++; postfix because it appears after) operator only gives the existing value before doing anything. That's why, when called, gives what exists, not what's the result of the addition.
Because of this, use the prefix operator instead, ++x, to get the value while still incrementing the variable.
The same applies to the decrement operator.
It's because using i++ returns i first, then adds 1 to i. If you call in1() the second time, you will see 11, 12 and so on.
Try using ++i instead.
When using the postfix syntax i++ the value is returned before incrementing. Use the prefix syntax instead ++i.
This question already has answers here:
What does i = (i, ++i, 1) + 1; do?
(7 answers)
Closed 4 years ago.
Could you tell me what this i means after the comma. I am struggling to understand this syntax or even find it in the internet.
let i;
this.questions = document.querySelectorAll('.question'), i;
i is not used anymore in the function
If setting a debugger after this lines. i is undefined
It doesn't seem to be of any use indeed.
As an information, it's just to declare / instantiate your variables. Take a look :
let i, j;
j = 1, i = 2;
console.log(i, j);
This question already has an answer here:
What does this symbol mean in JavaScript?
(1 answer)
Closed 6 years ago.
In this code, what does 'q--' mean in the while loop?
getTotal: function () {
var q = this.getItemCount(),
p = 0;
while (q--) {
p += basket[q].price;
}
return p;
}
Is this JS shorthand? Is there an online tool that converts shorthand JavaScript into longhand? Also, why are vars q and p declared this way instead of defining them this way:
var q = this.getItemCount();
var p = 0;
It's the decrement operator. The value of q is decreased by 1 each time q-- is evaluated but, importantly, the value is returned before the decrement.
So, the loop above will continue until q=1 but the value used inside the loop during this final iteration will be q=0.
In layman's terms: q-- means "Give me the value of q then decrease it by 1 directly afterwards".
as you can tell q is a variable with the number of items and the -- is Decrement Operator it just subtracts one form q until reaches 0.
This is works because in javaScript 0 == false and it will get out of the loop when q reaches 0.
Here, q-- means "subtract 1 from q and keep its old value q". The variables are declared in the shorthand method to reserve the size of the page and therefore its loading time.
This question already has answers here:
Why does [5,6,8,7][1,2] = 8 in JavaScript?
(3 answers)
Closed 7 years ago.
In a coding test I have recently encountered a question that asks me to find out what will be printed in console. The question is below. I did not understand how this below code will be understood and executed.
Need help
var arr = ["a" , "b" , "c" , "d"][1,2,3];
console.log(arr);
What does that arr definition even mean?
Arrays in JS are one dimensional.
So var arr = ["a" , "b" , "c" , "d"][1,2,3];
Here [1,2,3] represents the indexes.
And comma is the least preceded operator so [1,2,3] will return you 3 (i.e. The right most element always).
Therefore
The result will return you the value at index 3 that's d
Update:
Let's see some more points for comma operator:
From MDN:
The comma operator evaluates each of its operands (from left to right)
and returns the value of the last operand.
So, It means:
var a = (10, 18);
a; //18
Also note that it's essential to mention them inside brackets while assigning to a variable.
If you try something like:
var a = 1,2,3;
a; // will return 1
So it's important that you wrap them using ().
When will you use it , operator?
You can use the comma operator when you want to include multiple
expressions in a location that requires a single expression. The most
common usage of this operator is to supply multiple parameters in a
for loop.
for (var i = 0, j = 9; i <= 9; i++, j--)
Now that you've understood about comma operator, Let's see few more examples to test our understanding:
1. var a = (1,3)/(2,1);
a // what will be the o/p
2. var a = ["Stack", "OverFlow","is","Awesome"][1,3,2][2,0]
a // What will be the o/p
3. var a = (true,0,1), b, c;
if(b,c,a) {
console.log("Will it execute?");
}
Your example caught me offguard. Here's another example which illustrates what is going on. Essentially a list of comma separated values evaluates to the value of the last.
if (false, false, true) {
console.log("hello")
}
// --> "hello"
This question already has answers here:
Javascript expression in parentheses
(2 answers)
Closed 8 years ago.
I just came across some notation in JavaScript like so:
var a = (1,2,3,4,5);
This will always return the last value, in the above case 5. I'm aware of using brackets to namespace my JavaScript code, but have never seen it used this way.
Is there any use for this notation, or is it just some JavaScript byproduct?
It's the comma operator. As the mdn states (link) it always returns the later value. In your example it doesn't make much sense, since it will always assign a = 5. But consider this:
for (var i = 0, j = 9; i <= 9; i++, j--) {
...
}
It's used to increment and decrement in a single statement: i++, j--
Edit:
The parentheses in your example are necessary because its a variable declaration. In other cases they can be left out.
Parens are used to groups operations together. This is helpful for both setting operation precedence (e.g. x = (2+3) * 5 vs x = 2 + 3 * 5) and for making your code a little easier to read.
I suspect this is more a question about the comma operator. This is for making multiple assignments or operations on the same line. Here is a nice article about it: http://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/