JavaScript
Programming
Expressions
Code Analysis
Object Properties

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.

Browse interview questions

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.

javascript
1const obj = {
2  isReady: !flag,
3  value: -count,
4  type: typeof input,
5};

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.

javascript
1const obj = {
2  userName: user.name,
3  firstItem: list[0],
4};

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:

javascript
1const config = {
2  enabled: !settings.disabled,
3  title: page.header.title,
4};

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.

javascript
1const acorn = require("acorn");
2
3const ast = acorn.parse(
4  "const config = { enabled: !settings.disabled, title: page.header.title };",
5  { ecmaVersion: 2020 }
6);
7
8console.log(ast.body[0].declarations[0].init.properties.map(p => p.value.type));

The output is conceptually:

text
["UnaryExpression", "MemberExpression"]

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:

text
UnaryExpression
  argument -> MemberExpression

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.flag is a UnaryExpression whose argument is a MemberExpression.
  • 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

  • 'UnaryExpression means the value is produced by a unary operator such as !, -, or typeof.'
  • 'MemberExpression means the value is produced by property access such as obj.x or obj[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 UnaryExpression and MemberExpression at different levels.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.