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

JavaScript Short Circuit Evaluation

alex nonya
PLUS
alex nonya
Courses Plus Student 9,265 Points

Start = 0, doesn't work? why does that evaluate to false?

I'm not following why in the example:

function countToFive(start) {

start = start || 1;

for (var i = start; i <= 5; i +=1) {

console.log(i); } }

you can't call the function with, countToFive(0).

how is start = 0 false?

3 Answers

alex nonya , it would seem that in JavaScript, 0 will default to being false. There are several other things that will also default to false when you are testing for a boolean value (you can see those here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean).

When you are testing a condition with the or || operator, you are testing for a true/false statement (or a boolean value). As mentioned in these videos, when you are testing conditions with ||, once you hit the first false result, that's what you're returned.

So, when start is 0 || 1, it will be false and never get to the 1.

Hope that helps somewhat!

Steven Parker
Steven Parker
231,172 Points

It's the other way 'round. When testing with || (OR), a first false is skipped and the other value is returned. So with "start = 0 || 1" it will always get to the 1.

Thanks for correcting that, Steven!

Steven Parker
Steven Parker
231,172 Points

I'm not sure I understand the question. If you call "countToFive(0)", it will log the numbers 1-5 to the console.

The "start" would get reassigned to 1, since the expression "start || 1; evaluates to 1 when "start" is 0.

Please elaborate a bit more if that's not what you were asking.

alex nonya
PLUS
alex nonya
Courses Plus Student 9,265 Points

Thank you for helping!

"The "start" would get reassigned to 1, since the expression "start || 1; evaluates to 1 when "start" is 0."

Exactly that.....why would the expression evaluate to 1 when 'start' is 0. if 'start' being assigned 2 doesn't default to 1, why does 0 behave differently? What is different in setting 'start' to 2, and having the function count from 2 to 5, and setting 'start' to -2, and having it count from -2 to 5?

Steven Parker
Steven Parker
231,172 Points

When evaluated by the "or" operator, 0 is the only numeric value that is considered "falsey".

Steven Parker
Steven Parker
231,172 Points

alex nonya — If that answers the question, you can mark it solved by choosing a "best answer".
And happy coding!