The first script will try to load jQuery from an external website (in this case, Google CDN).
The second one will try to find the jQuery object in window
, and only if it doesn't find it, then it will load the library from an internal link. It's only a fallback in the case the CDN fails.
window.jQuery || document.write(...)
// the code above means the same as the code below
if (window.jQuery === undefined) document.write(...)
In Javascript, besides booleans (true/false), there are truthy and falsy values. Anything that's different to 0
, false
, undefined
and null
is a truthy value.
In that case, if the jQuery
property exists on window, it will be an object, and it will be a truthy value, so, the second statement won't run (because the first one is already true - and in an OR
operator, if any of the statements is true, it skips the others (from left to right).
Although, if the jQuery
property doesn't exist, it's because the first script didn't load correctly, and the property will be undefined
, a falsy value. So, the second statement will run, and the local jQuery will be loaded as a fallback.