George Mauer
George Mauer

Reputation: 122052

What does this complicated return statement do in javascript?

Browsing the d3 source code today I saw the following line:

return delay <= elapsed ? start(elapsed) : d3.timer(start, delay, time), 1;

I've been doing daily javascript for years and have never seen that before. What the hey?

Upvotes: 2

Views: 181

Answers (4)

iKlsR
iKlsR

Reputation: 2672

it means if the left hand operand is less than or equal to the right hand operand in nearly(if not all) languages

Upvotes: 1

loganfsmyth
loganfsmyth

Reputation: 161457

It is just less-than or equal. Maybe the Ternary operator combined with the comma operator is what is throwing you off?

This is equivalent to this:

if (delay < elapsed) start(elapsed);
else if (delay === elapsed) start(elapsed);
else {
  d3.timer(start, delay, time);
}
return 1;

Upvotes: 14

Philipp
Philipp

Reputation: 69663

<= means less-or-equal.

The complete line you posted means in plain english "if delay is less than elapsed, return start(elapsed), otherwise return the value of d3.timer.

Upvotes: 0

McGarnagle
McGarnagle

Reputation: 102743

Just means "less than or equal to".

Upvotes: 2

Related Questions