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>')
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>')
If jQuery is not undefined in the global object, then write a script element that will load jQuery from your server.
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>')
}
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'