Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript semantics

Tags:

javascript

I am trying upgrade my javascript programming skills ( or lets say my programming skills period : ) )

so I am trying to understand some semantics :

in the first line what does the "?" mean as well as the minus sign in "-distance"

in the second line what does '+=' or '-=" mean?

 el.css(ref, motion == 'pos' ? -distance : distance)

animation[ref] = (mode == 'show' ? (motion == 'pos' ? '+=' : '-=') : (motion == 'pos' ? '-=' : '+=')) + distance;

thank you

like image 259
salmane Avatar asked Jan 27 '10 09:01

salmane


2 Answers

a ? b : c means "b if a is true, c otherwise".

-a means a, negated.

a -= b and a += b mean a = a - b and a = a + b respectively. However, in your example these operators aren't actually present in the code, they are just text strings the code is manipulating.

like image 69
moonshadow Avatar answered Oct 06 '22 00:10

moonshadow


? is the ternary operator

it equals

if( motion == 'pos' ) { return -distance; } else { return distance; } // - is just negating the distance value
like image 40
roman Avatar answered Oct 05 '22 23:10

roman