November 20th, 2006

JavaScript: list of implicit true/false values

I got tired of sometimes wondering whether I could use a particular JavaScript value effectively in a conditional statement…
For instance, if I’m in the middle of a block of JS code, and am not 100% sure what the value of a variable will be, is it safe to say:

if (foo)

or do I need to explicitly look at the value of foo, like:

if ((typeof foo != 'undefined') &&
(foo != null) &&
(foo != ''))

So, I took a little time one day, recently, and checked how various values are evaluated in JavaScript. I believe I gathered this info using IE6. Hopefully that fact is not important. Might be worth checking in Firefox one of these days, just to be sure.

false false
0 false
0.0 false
null false
false
undefined false
NaN false
true true
Infinity true
‘ ‘ true
1 true
-1 true
2 true
-2 true
0.1 true
‘0′ true
‘1′ true
‘-1′ true
‘null’ true
‘true’ true
‘false’ true
‘TRUE’ true
‘FALSE’ true

October 23rd, 2006

MS Word’s Find and Replace has limited regex support

I just noticed a post on TGAW’s blog, Word 2003 - The Find What text contains a Pattern Match expression which is not valid, which contained some information that I don’t need right this second, but I do want to make a note of that’s more conspicious to me in the future…

She was trying to do a search/replace in Microsoft Word 2003, and stumbled across the fact that the Find and Replace dialog, with the ‘Use Wildcards’ option checked, “is pretty much a regular expression search (it does appear to be missing some support like \d, \w, etc)”.

I’m a big fan of regular expressions (although I must admit having been intimidated by them for years before finally getting comfortable using them), and often post code samples that used regexes, and just plain search/replace expressions that have proven useful to me, on my old geek blog (and in the future they will go here instead).