Truth and Equality in JavaScript

This is a subject much covered in JavaScript programming annals. Here is a beginner’s link for those following in Fraffic’s taillights.

This is from Douglas Crockford’s JavaScript: The Good Parts:

For those who’ve never seen an ‘if’ statement in railroad diagram form

The if statement changes the flow of the program based on the value of the expression. The then block is executed if the expression is truthy; otherwise, the optional else branch is taken.

Here are the falsy values:

  • false
  • null
  • undefined
  • The empty string ”
  • The number 0
  • The number NaN

All other values are truthy, including true, the string ‘false’, and all objects.



Inspector Gadget’s cousin SelectorGadget

Speaking of Bookmarklets, I came across this very cool one the other day: SelectorGadget. SelectorGadget is an open source bookmarklet that provide CSS selector information about DOM objects on a web page.

It can be found here:

The selector info it provides can be used in jQuery. This makes getting the right syntax for querying the DOM a snap.

Check out the author’s screencast on the front page.

Bookmarklets, oh my!

Well, I encountered bookmarklets last week. These are JavaScript snippets which can be executed from the Bookmarks menu in a web browser. The snippets operate on the current page and can be used to scrape data or manipulate info gotten from the page.

Bookmarklets are created using the URI scheme with the protocol type ‘javascript:’ (instead of http: or ftp:, for example).

A common way to use the bookmarklet is to provide it in the form of an href element which you then drag to the bookmarks menu in the web browser.

<a href="javascript:(function(){alert('Fraffic says hello!');})();">drag this to the add bookmark icon on your browser</a>

Unfortunately, Word Press doesn’t seem to play well with inline JavaScript, so I can’t provide you will a live demo.