Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For loops question #46

Closed
DjebbZ opened this issue Feb 19, 2012 · 5 comments
Closed

For loops question #46

DjebbZ opened this issue Feb 19, 2012 · 5 comments

Comments

@DjebbZ
Copy link

DjebbZ commented Feb 19, 2012

You say

substitute i++ with i = i + 1 or i += 1 to avoid excessive trickiness

So why not substituting i-- with i = i -1 or i -= 1 in the 2 preferred methods ?

@Feodoric
Copy link

Agreed with this, seems you are relying on 2 types of trickiness for the i-- examples: 0 being falsy, and i-- returning the value of i before the decrement operation.

@chuanxshi
Copy link
Owner

@DjebbZ @Feodoric problem is, if you try i = i -1 or i -= 1 with an array, it will not return the first item in the array.

@DjebbZ
Copy link
Author

DjebbZ commented Mar 30, 2012

I understand. In the last loop iteration, after i-- is evaluated, i equals 0, which is falsy. Can you confirm ?

@antonywu
Copy link

antonywu commented Apr 3, 2012

Typically, I don't even do i--, I do --i instead
Crockford roughly went through the reason with "++" in http://www.youtube.com/watch?v=taaEzHI9xyY&t=50m42s

What makes "++" a special case is that "+" is also used for string concatenation, so I suppose it is easier to make mistake. Coming from C++ world, I don't see it as a big deal. However, both Crockford and I would agree that ++i and --i are not the same as i++ and i-- (the former usage is preferred, whereas later should be avoided)

@DjebbZ
Copy link
Author

DjebbZ commented Jun 21, 2012

Thanks for the answers. Closing since it's been open for a long time.

@DjebbZ DjebbZ closed this as completed Jun 21, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants