Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "a + + b" work, but "a++b" doesn't?

Tags:

javascript

I was fiddling around with different things, like this

var a = 1, b = 2; alert(a + - + - + - + - + - + - + - + b); //alerts -1 

and I could remove the spaces, and it would still work.

a+-+-+-+-+-+-+-+b 

Then I tried

a + + b 

It ran and evaluated to 3, but when I removed the spaces, (a++b) it wouldn't run, and it had a warning which read "Confusing plusses."

I can understand that in cases like

a+++++b 

which could be interpreted as any of the following

(a++) + (++b) (a++) + +(+b) a + +(+(++b)) a + +(+(+(+b))) 

that it would be confusing.

But in the case of

a++b 

the only valid way to interpret this, as far as I can tell, is

a + +b 

Why doesn't a++b work?

like image 713
Peter Olson Avatar asked Sep 20 '11 21:09

Peter Olson


People also ask

Why ab exercises dont give you abs?

Having Strong, Muscular Abs is Not Enough In order to have defined abs or a six pack, you need to get rid of subcutaneous fat from your abdominal area. Bottom Line: Exercising your abs will help them become strong and muscular. However, you won't be able to see them if they're covered by subcutaneous fat.

Are ab workouts actually effective?

Doing Ab Exercises Doesn't Get Rid of Abdominal Fat While exercising the muscle may increase endurance or strength, it won't burn off the fat in that area. 2 The reason for this is because the body draws energy from the entire body when exercising, not just from the part you're working on.

Why do my abs not hurt after a workout?

As your body gets stronger, and your muscles adapt to the new type of movement, you won't feel the soreness afterwards.

Why am I not seeing results from my ab workouts?

1. You have too much body fat. The single most important aspect when it comes to getting your abs to show is having a low body fat percentage. All humans have abdominal muscles that can be made more visible with training – but ultimately to see your abs you need to be at 10% body fat or less (18% or less for women.)


1 Answers

The Javascript parser is greedy (it matches the longest valid operator each time), so it gets the ++ operator from a++b, making:

(a++) b 

which is invalid. When you put in spaces a + + b, the parser interprets it like this:

(a) + (+b) 

which is valid and works out to be three.

See this Wikipedia article on Maximal munch for more details.

like image 71
Digital Plane Avatar answered Oct 03 '22 10:10

Digital Plane