Proper use of ||
javascript
Solution
It returns the item on the left if and only if it is truthy.
The following are not truthy:
- The primitive boolean value `false`
- The primitive string value `""` (the empty string)
- the numbers +0, -0 and `NaN`
- the primitive value `null`
- the primitive value `undefined`
Everything else is truthy.
Here is the list on the language specification.
In your case `cache[0]` returns 0 which as we can see is falsy so it enters recursion. This is why we avoid `||` for short circuiting in these situations.
You should consider checking directly that the object has that property: `number in cache` is one such way and another is `cache[number] !== undefined`.
Problem
The general question I suppose is: when does || return the item on the left, and when does it return the item on the right? The specific question, is why doesn't this work: ``` var fibonacci = (function () { var cache = [0, 1]; function fibonacci(number) { return cache[number] = cache[number] || (fibnonacci(number - 1) + fibonacci(number - 2)); } return fibonacci; })(); var $div = $('div'); for (var index = 0; index < 10; index++) { $('<span />').text(fibonacci(index)) .appendTo($div); } ```