JavaScript evaluates the number 0 as false: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
So, as ae pointed out, if window.scrollY is 0 then it will be seen as false. This will cause your expression to evaluate window.pageYOffset, so the final result will be undefined instead of 0 as it should be.
In order to get around this problem you could use a ternary expression such as:
var top = typeof window.scrollY === “undefined” ? window.pageYOffset : window.scrollY;