help me to understadn increment ?

Recommended Posts


i don't understand the principle of increment with javascript. This snippet works well :

//in update
	if (obj.scale.x < 8) {
		obj.scale.setTo(obj.scale.x + speed)

but this example don't work however it's supposed to be the same...why that don't works ?

// in update
	if (obj.scale.x < 8) {
		obj.scale.setTo(obj.scale.x++ * speed)


Share this post

Link to post
Share on other sites

Your 2 examples aren't the same, and I can't see why you'd think they would be comparable. I can see why you'd be confused by the output of the 2nd though, but if you are confused by the output then its to do with the order of operations.

Postfix operators (generally) have lower precedence so they happen after other operands, prefix operators (generally) are executed before.


var x = 10

x++ + 2
=> 10 + 2 => change x
// 12, x === 11

x = 10

++x + 2
=> change x => 11 + 2
// 13, x === 11

I'm not sure on the exact order of operands in JS, but I'd pretty much expect prefix to happen first and postfix to happen last in most expressions.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.