undefined cast to a string with regex test

Rick Waldron waldron.rick at gmail.com
Wed Oct 10 05:11:29 PDT 2012



On Wednesday, October 10, 2012 at 6:19 AM, gaz Heyes wrote:

> This wasn't totally unexpected but seriously made me go WTF. It appears that arguments to regexp.test are cast to a string. This was bad for me since I was writing a js parser at the time and checking the argument to see if it matched one of the reserved words. See the example below:-
> 
> if(/^(?:undefined|ok|wtf|js)$/.test(undefined)){
>   alert('WTF');
> } else {
>    alert('NO WTF');
> }

This is by design and surely not a wtf. test() is specified to accept and operate on a string argument and therefore coerces it's arg to a string.

This is very common throughout ES

Rick 
> 
> That's pretty much all I got but if you didn't go WTF then I apologise for the wasted thread.
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> 
> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121010/91e15f2f/attachment.html>


More information about the es-discuss mailing list