salmane
salmane

Reputation: 4849

javascript semantics

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

Upvotes: 6

Views: 1911

Answers (5)

Andrew
Andrew

Reputation: 5277

Here is a link that will answer the ? question (? is a shorthand evaluation operation). http://www.w3schools.com/JS/js_comparisons.asp

+= would be used to increment a value (also shorthand) e.g.

i = i + 1; is the same as i += 1;

the same applies to -=

Upvotes: 0

Ivan Karpan
Ivan Karpan

Reputation: 1554

What you call 'semantics' is actually programming language syntax. It's very basic knowledge that can be acquired easily by googling a bit or looking at Wikipedia.

Here's the JavaScript article on Wikipedia, and here's the answers on your first (conditional operator section), second (Arithmetic) and third (Assignment) questions within the same article. RTFM please.

Upvotes: 0

kennytm
kennytm

Reputation: 523304

  1. (a ? b : c) means "return b if a is true, and return c if a is false."
  2. The minus sign means negation.
  3. The '+=' and '-=' are simply strings.

Upvotes: 1

moonshadow
moonshadow

Reputation: 89065

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.

Upvotes: 5

roman
roman

Reputation: 11278

? is the ternary operator

it equals

if( motion == 'pos' ) { return -distance; } else { return distance; } // - is just negating the distance value

Upvotes: 1

Related Questions