Search code examples
javascripttypeof

What's the meaning of UnaryExpression of ES6 Spec documentation?


I'm curious about the meaning of UnaryExpression of ES6 Spec documentation. https://www.ecma-international.org/ecma-262/6.0/#sec-typeof-operator

Actually I was wonder about why typeof operator could accept both operand without parenthesis, also operand with parenthesis.
ex) typeof a / typeof (a)

So I searched on the internet and found ES6 Spec documentation guide me that typeof operator could accept UnaryExpression.

UnaryExpression : typeof UnaryExpression

and also found out this SO answer.

https://stackoverflow.com/a/15843896/911528

In this article, user Halim Qarroum answered,

The correct definition for typeof operator in the specification is : typeof[(]expression[)]

I don't know the exact meaning of UnaryExpression. (I couldn't find out. Yes the definition is there but they are so abstract.)

And I'm curious how Halim Qarroum could infer that answer through only see the spec documentation.

Does [(]expression[)] is equals to UnaryExpression? Does [] means omit-able?


Solution

  • A unary expression is an expression with only one token. Unary Operators (such as typeof or ++) take as an operand a unary expression.

    Parenthesis, when wrapping a unary expression, are ignored by the parser. Hence, typeof(i) and (i)++ are equivalent to typeof i and i++, respectively.

    Yes, in developer documentation square brackets denote optional things. usually used for optional function arguments, but understandable in the answer you quoted.