Welcome to the Treehouse Community
Looking to learn something new?
Chris Kwong10,266 Points
I was looking at some code in a forum and this:
current = newGallery || 0;
popped up. I think it means: if newGallery is not null or undefined, set it to 0, otherwise current = newGallery.
What is this syntax? Is there an equivalent with &&. I only know of || and && as boolean OR and AND
Well, it's my understanding that || is going to be that Boolean "or," so this is going be "set current to either the variable called newGallery or zero," I believe!
It's a good idea to do a test to see how this works. It's not specifically looking for 'null' or 'undefined' it's actually looking for truthy/falsey. James Padolsey has an excellent write up on Truthy & Falsey worth reading.
In JS there are 5 falsey values: undefined, null, NaN, 0, "" (empty string), and false, of course.
So in a simple test we can look at a truthy equivalent:
// Truthy var newGallery = 1; var current = newGallery || 'false'; alert(current); // Alert returns 1
// Falsey var newGallery = 0; var current = newGallery || 'false'; alert(current); // Alert returns false