# What is a Reference Record?

**URL:** <https://es.discourse.group/t/what-is-a-reference-record/1115>\
**Category:** Spec Reading\
**Created:** [December 5, 2021, 4:33am UTC](https://es.discourse.group/t/what-is-a-reference-record/1115 "2021-12-05T04:33:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hongji00](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/hongji00/32/1147_2.png) [@hongji00](https://es.discourse.group/u/hongji00)\
**Post date:** [December 5, 2021, 4:33am UTC](https://es.discourse.group/t/what-is-a-reference-record/1115/1 "2021-12-05T04:33:58Z")

</div>

I'm reading [https://tc39.es/ecma262/multipage/ecmascript-data-types-and-values.html#sec-reference-record-specification-type](https://tc39.es/ecma262/multipage/ecmascript-data-types-and-values.html#sec-reference-record-specification-type). Document says left-hand operand of an assignment is expected to produce a Reference Record.  
Is Reference Record part of Environment Record or something?

---

<div class="post-metadata">

**Author:** ![jschoi](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jschoi/32/917_2.png) [@jschoi](https://es.discourse.group/u/jschoi)\
**Post date:** [December 5, 2021, 6:39am UTC](https://es.discourse.group/t/what-is-a-reference-record/1115/2 "2021-12-05T06:39:36Z")

</div>

No, an Environment Record doesn’t contain Reference Records. The Environment Record contains variable _bindings_.

Reference Records can be references _to_ those variable bindings in the Environment Record. In that case, they’re _pointing to_ the bindings inside of the Environment Record.

Basically, variable references (like `x`) and property accesses (like `x[y]`, `x.a`, and `super.a`) all evaluate into Reference Records. In contrast, other expressions like a number literal `42` or a function call `x.a()` do not evaluate into Reference Records.

Reference Records are usually invisible, because Reference Records usually turn into the values that they wrap. They are not invisible when they are the left-hand side of an assignment (as well as a couple of other operations like `delete`).

Imagine this:

```js
const x = 5;
console.log(x); // Line A
x = 3; // Line B

```

What does `x` evaluate into, in Line A? What about in Line B?

In Line A, `x` gets evaluated into a Reference Record (a reference to the variable called `x`), and because the `console.log` function call needs the Reference Record’s value, the Reference Record gets “dereferenced” into `5`.

In Line B, `x` gets evaluated into a Reference Record (a reference to the variable called `x`)…but the assignment operation does _not_ dereference it into `5`. It uses the name of the variable (which is in the Reference Record) to assign the new value `3`.

In _both_ lines, `x` evaluates into a Reference Record, but what happens to that Reference Record afterward depends on what operation it’s inside.

Most operations will dereference the value inside the Reference Record’s variable (or property chain or whatever). But assignment uses the name inside the Reference Record instead of dereferencing the Reference Record.

(In order to dereference a variable Reference Record, you need the variable’s name and the lexical environment that it’s from—its Environment Record. The variable’s name is stored in the Reference Record’s [[ReferencedName]] field, and the Environment Record that owns the variable is stored the Reference Record’s [[Base]] field.)

Now imagine this:

```js
const x = 5;
console.log(x["a" + "b"]); // Line A
x["a" + "b"] = 3; // Line B

```

What does `x["a" + "b"]` evaluate into, in Line A versus Line B?  
In _both_ lines, `x["a" + "b"]` evaluates into a Reference Record for a property access. Its base is another Reference Record (for the variable called `x`) and its [[ReferencedName]] is `"ab"`.  
In Line A, that property-access Reference Record gets dereferenced by the `console.log` call into `5`.  
In Line B, that property-access Reference Record does not dereference the assignment operation; instead, the assignment operation uses the property-access Reference Record to figure out where to assign the new value `3`.

In summary, a Reference Record is either:

- A variable reference (in which case its [[Base]] is the current Environment Record and its [[ReferencedName]] is the name of the variable) or
- A property access (in which case its [[Base]] is the base of the property access and its [[ReferencedName]] is the name of the property—a string, a `#privateField`, or a symbol).

I’m simplifying things, but this is how I think of them.

---

<div class="post-metadata">

**Author:** ![hongji00](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/hongji00/32/1147_2.png) [@hongji00](https://es.discourse.group/u/hongji00)\
**Post date:** [December 5, 2021, 9:24am UTC](https://es.discourse.group/t/what-is-a-reference-record/1115/3 "2021-12-05T09:24:39Z")

</div>

Thank you for your detailed description! helped a lot
