Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ben Os
20,008 PointsA question to Andrew Chalkley regarding the short circuit evaluation workshop
Hi Andrew,
In here in about 6:00 to 10:40, https://teamtreehouse.com/library/short-circuit-evaluation
I didn't quite succeed to understand why you used age && age... Given there is only one parameter named "age" and argued as 18, I don't understand why we have age && age...
Repeating a variable seems to me as something usually avoided in programming and quite unique to this example, but I really can't explain why you picked that particular example.
Would thank you for an explanation in other words... Ben.
3 Answers
Joel Kraft
Treehouse Guest TeacherHi Ben,
The JavaScript interpreter "reads" logical expressions from left to right. But it's kind of lazy. If it sees that it doesn't need to read all the way to the end of a line to know what the value is, it won't. So for the expression
age && age >= 18
where age is undefined, the first thing it sees is undefined &&, which is almost the same thing as false &&.
Because both sides of the && have to be true for the value of the entire expression to evaluate to true, it doesn't matter whether the second half is true or false. The expression will be false either way. So the JavaScript interpreter doesn't care about what comes after the &&. It stops reading and just moves to the next line.
The short-circuiting analogy is just saying the distance the interpreter has to "read" is shorter. If it helps to think of lines of code as legs of a circuit, a leg that uses short circuit evaluation is potentially shorter, thereby skipping the "resistance" of evaluating code unnecessarily. If that analogy doesn't make sense for now, it's not too important, as long as you understand the way the optimization works.
Does that help?
Ben Os
20,008 PointsI think I understand why you did that Andrew - It seems to me like some kind of a conditioning I've yet to seen, as it says:
return age && age >= 18;
If indeed it means "return age (while) age >= 18", then it seems && takes the form of an intermediate between the parameter (age) to the argument, and thus even if we argue to the parameter something which is different from the intermediate (18) it won't work...
In other words, If we set the argument to be 15, as you did, while the intermediate is 18, then it won't work.
Ben Os
20,008 PointsI was surprised to discover this whole "intermediate" concept, as an intermediate between a param and an arg.
Ben Os
20,008 PointsI now see it as cool tweak allowed by the nature of the Short Circuiting logic.
Alexander Davison
65,469 Pointstagging Andrew Chalkey
Andrew Chalkley
Treehouse Guest TeacherYeah the first age is checking for the existence if it does exist we can then do the second check - if they are an adult.
If the age is falsy i.e. undefined the computer program doesn't need to evaluated the remainder of the expression, it can just return therefore short-circuiting the expression.
Ben Os
20,008 PointsAndrew, I think I didn't understand what you mean in the last sentence: If the age exists, but undefined, then the SCE is true-false and the the second return will be executed (return false) instead of the first one (return true).
Is that what you mean in that sentence ?
Andrew Chalkley
Treehouse Guest Teacherreturn falsy && true
Will return the falsy value (in the case of it not existing, undefined). There is no need for the computer program to check the condition of the the second part of the expression (i.e. not wasting computation cycles) this is why it's called short-circuiting the rest of the expression.
Ben Os
20,008 PointsAndrew, I have only one question about your last sentence: "this is why it's called short-circuiting the rest of the expression."
As I understand the program itself is a circuit and our topic is called short circuiting as it is a circuit ---> Inside the program circuit, but it usually is very short (usually 2-4 operators like or/xor/and).
If I am wrong and you meant to say otherwise can you please clarify your last sentence for me? Thank you !!!
Andrew Chalkley
Treehouse Guest TeacherA "short circuit" in electronics is if there's water or another conductive material that causes a part of the circuit to cease to function. The current takes the shorter route. In the case of "short circuiting" in programming the code takes a shorter path because it doesn't execute the full program as it's not required. This is why it's called short circuit evaluation.
Ben Os
20,008 PointsThe current takes the short path as it goes along with the water and not on the regular route? The circuit then becomes shorter and scattered/dissolved?
The program will continue anyway with it's logical flow even if the condition inside the parenthesis will be false, so I think I fail to understand the parallel...
Ben Os
20,008 PointsBen Os
20,008 PointsHi dear Joel! As a matter of fact it does make sense to me and help me very much;
I am convinced I now fully understand the meaning:
Short circuiting is potentially shorter as unlike the flow of the program itself, it generally won't continue when it reaches a false value and will exit the parenthesis of the "short circuit" to continue working with the "long circuit" which is the program itself as with it's logical flow.
Ben Os
20,008 PointsBen Os
20,008 Points"Potentially shorter", in contrast to "potentially longer", due to in SCE, it will generally stop evaluating when false, and in LCE, it will generally continue evaluating when dalse. A nice way for me to display the diff.