-2

I am using this code in my project but don't understand the purpose of operator (||) in the code

window.jQuery || document.write('<script src="js/libs/jquery-1.9.0.min.js">\x3C/script>')
j08691
  • 204,283
  • 31
  • 260
  • 272
Goatman
  • 7
  • 3
  • It's checks for a global jQuery object, and if it's not found, attempts to load it. See http://stackoverflow.com/questions/1828237/check-if-jquery-has-been-loaded-then-load-it-if-false – j08691 Jul 28 '14 at 19:38
  • Possible duplicate: http://stackoverflow.com/questions/2100758/javascript-or-variable-assignment-explanation – This company is turning evil. Jul 28 '14 at 19:40

3 Answers3

0

If jQuery is not undefined in the global object, then write a script element that will load jQuery from your server.

Ryan
  • 14,392
  • 8
  • 62
  • 102
0

This is a conditional inclusion. It is merely shorthand for:

if ( window.jQuery ) {
} else {
    document.write('<script src="js/libs/jquery-1.9.0.min.js">\x3C/script>')
}
B2K
  • 2,541
  • 1
  • 22
  • 34
0

In this kind situation || operator used to check, if window.jquery is true (exists) than use it, otherwise call document.write('\x3C/script>') to append it.

var name = newName || 'Mike';
console.log(name) // will display Mike 

because newName variable does exist, but if I do like this

var newName = 'John';
var name = newName || 'Mika';
console.log(name) // will display John

because newName variable exists and its value is 'John'

midan888
  • 179
  • 6