r/learnjavascript • • Jun 24 '13

Learn JS Properly - Week 3 (Slower week!)

Hi, everybody! I know last week's assignment was pretty heavy, so I set this week up to be easier.

ASSIGNMENTS:

0. Catch up on last week's assignments if you need to.

  1. Read either Chapter 8 of JS: The Definitive Guide or Chapter 7 of Professional JS for Web Developers.
  2. Do the Try jQuery course. This takes about 3 hours; I'd recommend doing it all at one time.
  3. Do all 5 of the Basic Projects on Codecademy.
  4. Download a trial copy of Webstorm. Then read this blog post and set up Webstorm.

EXTRA CREDIT:

21 Upvotes

19 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Jun 29 '13 edited Jun 29 '13

The short answer is that it tries EVERYTHING.

It's using the nature of the || operator.

When JavaScript tests something with the OR operator (||) it "shortcuts". If the first condition evaluated to TRUE, it stops evaluating, as it knows the OR will be true. Only if the first part is false (in the case of OR) does the second part get evaluated.

In this case, it calls +5 first, and only if that returns null does the *3 call get made. This happens in each branch, so for the *3 call to be made at the first level, ALL the branches of the + 5 call have returned null.

Think of a maze. At each intersection you can go left or right. One way to find the exit is to go left every time you reach an intersection until you reach a dead end. Then you back up 1 intersection and go right instead. That's what's happening in this function.

The && operator works in reverse, if the first operand evaluates to false, JS stops evaluating the rest of the expression.

1

u/danmofo Jun 30 '13

This makes much more sense - thanks!