Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Unexpected value when using "<" Date: Mon, 24 Nov 2014 03:37:02 +0100 Organization: PointedEars Software (PES) Lines: 69 Message-ID: <2134606.TczYAlEYiD@PointedEars.de> References: <723cd8aa-caff-4530-9977-9884cddeb3c7@googlegroups.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1416796646 29287 eJwFwQkBwDAIA0BLtBAeOYwS/xJ2B/XjE+ZwA0FJsq4U9X3orbRYUoahvUd6AE8zYb4b1vsDJ64RcQ== (24 Nov 2014 02:37:26 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Mon, 24 Nov 2014 02:37:26 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwNyEcBwDAMBDBKdjxygePJH0Krp0ycva66udraCk1R39Bj0lnFHQ/0gBhzYpxkDRu0PFjjH2AlJ0toZfkDUEEVYA== Cancel-Lock: sha1:kSELzHcTdmSlz9oWDTlQEKaql6Y= X-NNTP-Posting-Host: eJwNyMEBgDAIA8CViJSg45TU7D+C3vMqCaoXi6tcVjv7KYd9iH2NTpyY7kQA3Hw1DN2sv1P4ACX3ERg= Xref: csiph.com comp.lang.javascript:26202 RobG wrote: > Given the comparison: > > 'foo' < 2 > > I was expectingt the result to be *undefined*, based on the abstract > relational comparison algorithm[1]. A relational expression never evaluates to the “undefined” value; its evaluation result is (as it must be) of type Boolean. > In step 3a, the left hand side is converted to a number using > ToNumber('foo'), which returns NaN. Correct. > Step 3c of the algorithm says: > > If nx is NaN, return undefined Also correct. > So I was expecting the result to be *undefined*. The introduction to the > section also says that the comparison: > > "...produces true, false, or undefined (which indicates that at least > one operand is NaN)". > > However, in the couple of browsers I tested, the result was Boolean false. > Can someone please explain, or point to a suitable explanation elsewhere, > why the result is *false* and not *undefined*? You have overlooked that the Abstract Relational Comparison Algorithm does not terminate the algorithm of the operator: | 11.8.1 The Less-than Operator ( < ) | | […] | 5. Let r be the result of performing abstract relational comparison | lval < rval. (see 11.8.5) | 6. If r is undefined, return false. Otherwise, return r. IOW, *that* algorithm always returns either “true” or “false”. ∎ Apropos: The ECMAScript Support Matrix includes – and will probably use in the future – a draft reference implementation of ECMAScript Ed. 5.1 (and probably later of Ed. 6) in ECMAScript which supports a trace mode that is intended to make it easier to see such behavior. It does not include an implementation of the less-than operator or the Abstract Relational Comparison Algorithm yet. You can enable trace mode with the assignment “es.enableTrace = true”. Specification-related symbols are properties of the object referred to by “es”, so you can see what is implemented by typing “es.” in most script consoles already (but at the point not necessarily fully). Selecting an item from the usual auto-complete list would display the source code of the algorithm implementation (without comments). The entire source code can be found in [2] (which I will switch to Git along with the rest of my public repositories). Feedback and contributions are appreciated. > 1. http://ecma-international.org/ecma-262/5.1/#sec-11.8.5 [2] -- PointedEars FAQ: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.