Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: x === x Date: Wed, 31 May 2017 21:52:31 +0100 Organization: A noiseless patient Spider Lines: 68 Message-ID: <87efv4zr3k.fsf@bsb.me.uk> References: <874lw231ui.fsf@bsb.me.uk> <2872662.aeNJFYEL58@PointedEars.de> <87inkh28sf.fsf@bsb.me.uk> <19437984.EfDdHjke4D@PointedEars.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="04e45becd6848d7651d9f559a5a1bcb5"; logging-data="25362"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19nU57ScX8RSBVKDoQI+4KeuDqum72csVc=" Cancel-Lock: sha1:8Zh+5Ixse9nafuyMlm+6KKAdYxU= sha1:kSU9tceQvi7EaBerKz5dwE8IUhA= X-BSB-Auth: 1.0387a58d4e4c0bf69267.20170531215231BST.87efv4zr3k.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:33458 Thomas 'PointedEars' Lahn writes: > Ben Bacarisse wrote: > >> Thomas 'PointedEars' Lahn writes: >>> Stefan Ram wrote: >>>> Ben Bacarisse writes: >>>>> What's wrong with {} === {}? >>>> It depends on the context. >>> No, it does not. “===” *makes* the (expression) context. >> >> $ nodejs -c -e '[] === [];' && echo "ok" >> ok >> $ nodejs -c -e '{} === {};' && echo "ok" >> [eval]:1 >> {} === {}; >> ^^^ >> >> SyntaxError: Unexpected token === >> >> $ nodejs -c -e '({} === {});' && echo "ok" ✘ >> ok > > Fascinating. I had tried in the Chrome DevTools Console, and there the > trailing semicolon makes the difference: > > | > {} === {} > | <- false > | {} === {}; > | (x) Uncaught SyntaxError: Unexpected token === > > I assume that the explicit semicolon there is considered an /EmptyStatement/ > that switches back to statement context. I can't find any reference to "statement context" or "expression context" in the language standard. I assume it's your own terminology, but I don't see how it helps explain why [] === []; is OK but {} === {}; is a syntax error. (I know why, I'm just wondering how talking about === making expression context and ; switching back helps to explain this difference.) Just {} === {} both on it's own and with a semicolon, is a syntax error because it is not a /Script/ (or a /Module/). It's not a /Script/ because none of the options for a /Statement/ apply. There is an explicit exemption that prevents an /ExpressionStatement/ (the only reasonable candidate[1]) from staring with a {. On the other hand [] === [] (again, with or without a semicolon) does parse as a /Script/ because it *is* an /ExpressionStatement/. The trouble with testing witch a REPL is that there is no standard that mandates what the reader in such a loop should be expecting. [1] Unfortunately, when explaining why something isn't valid syntax, you should, formally, say why it is not any of the possible options. I hope it's clear that I'm just applying a short-cut by talking about "the only reasonable candidate". It's obviously not a /BlockStatement/ or a /VariableStatement/ or a ... and so on but, were it not for the exemption, it looks most like an /ExpressionStatement/. -- Ben.