Why are some object properties UnaryExpression and others MemberExpression?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If you are looking at a JavaScript AST and some values appear as UnaryExpression while others appear as MemberExpression, the parser is not classifying properties arbitrarily. It is describing the syntax node that produced the value on the right-hand side.
In other words, the AST is not asking "is this inside an object property?" It is asking "what kind of expression is this value?" A property value can be a literal, a function call, a unary expression, a member expression, or many other node types.
What UnaryExpression Means
A UnaryExpression is an expression with a unary operator applied to a single operand. Common examples are negation, logical not, typeof, and void.
If you parse those property values, the AST nodes for !flag, -count, and typeof input are all UnaryExpression nodes because the syntax starts with a unary operator.
What MemberExpression Means
A MemberExpression represents property access on some object-like value. Dot notation and bracket notation both fall into this category.
Here, user.name and list[0] are MemberExpression nodes because they access members of another value.
The Property Value Decides The Node Type
Consider this object literal:
The property enabled has a UnaryExpression value because !settings.disabled starts with !. The property title has a MemberExpression value because page.header.title is property access.
That distinction often becomes clearer when you inspect the AST with a parser such as Acorn or Babel.
The output is conceptually:
Nested Expressions Still Keep Their Own Types
AST nodes can contain other AST nodes. In !settings.disabled, the outer node is a UnaryExpression, but its argument is itself a MemberExpression because settings.disabled is property access.
That means both types can appear in the same expression tree:
This is a common source of confusion. People often look at the inner settings.disabled part and expect the whole property value to be a MemberExpression, but the outer operator changes the top-level node type.
Think In Terms Of The Outer Node
When you inspect an AST, always classify the expression from the outside in. The outermost syntax decides the node type you see first, and inner expressions only appear as nested children underneath that outer node.
Common Pitfalls
- Assuming the AST node type is determined by the object property rather than by the value expression.
- Forgetting that
!obj.flagis aUnaryExpressionwhose argument is aMemberExpression. - Reading only part of a nested expression and missing the outermost operator.
- Expecting all property values in an object literal to have the same node type.
- Confusing property definitions in an object literal with property access expressions elsewhere.
Summary
- '
UnaryExpressionmeans the value is produced by a unary operator such as!,-, ortypeof.' - '
MemberExpressionmeans the value is produced by property access such asobj.xorobj[key].' - Object property values can be any valid expression type.
- The top-level AST node type is determined by the outermost syntax of that value.
- Nested expressions can contain both
UnaryExpressionandMemberExpressionat different levels.
Related reading
- Why async/await doesn't work in my case?
- Why await is not working for node request module?
- Why 'await' requires 'async' in function definition
- Why can I not throw inside a Promise.catch handler?
- Why chrome console is not returning undefined here?
- Why do my vue-router links sporadically lead to the wrong page or don't work at all?
- Why do we need middleware for async flow in Redux?
- Why do we need middleware for async flow in Redux?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.