Reading an article on NetTuts+ by Jeffrey Way made me intrigued by the concept of this. I haven't ran any testing to check if an if() statement or this method runs faster... but it's interesting nonetheless.
For users who don't want to read the article, Jeffrey basically states that with JS when using the AND operator, the second statement only runs if the first statement is true.
So:
b===10 will only run if a=5. With this in mind, other methods become available to us, such as:
This will check if an element exists with an ID of contents. If it doesn't, it will create a DIV with an ID of contents.
A more real-life example was also given:
which downloads a local copy of jQuery if the copy was not retrievable from the CDN for whatever reason.
I found it an interesting read and decided to share it
For users who don't want to read the article, Jeffrey basically states that with JS when using the AND operator, the second statement only runs if the first statement is true.
So:
Code:
var a=5;
b=10;
if((a===5) && (b===10)){
alert('yay');
}
Code:
!document.getElementById('contents') && createElem('div', 'contents', 'hello world');
A more real-life example was also given:
Code:
!window.jQuery && document.write('<script src="localjQuery.js"><\/script>');
I found it an interesting read and decided to share it