The ? in JS

Alright this is just something I figured how to do recently and I figured I would share it with other JS developers.

 Ever see code like….

var component = e.target?e.target:e.srcElement;

Generally the question that comes to mind what the heck does the question mark mean? Well, belive it or not it’s a short hand If statement.

Basically in this case it’s like saying if e.target exist then set variable targ to e.target. If not, “ELSE”, make targ equal to e.srcElement. This is really useful for cross browser issues.

Sadly though, you can’t really use it in the AJAX code all that easily. Due to how many conditions you have to test against. But it’s really useful for other issues.

Also to clear some things up:

a=(a==b)?c:d

You can also write it out like that to mean… if a = b then set to c if NOT set to d.

Well I hope this helps some of you guys. I'll keep you guys posted.