# JavaScript Code Tracing Practice for Developers

> Trace JavaScript execution by following values, aliases, mutations, closures, and asynchronous boundaries instead of relying on a top-to-bottom impression of the code.

- Canonical HTML: [https://unrust.dev/languages/javascript/code-tracing](https://unrust.dev/languages/javascript/code-tracing)
- Markdown representation: [https://unrust.dev/languages/javascript/code-tracing/index.md](https://unrust.dev/languages/javascript/code-tracing/index.md)

Published: 2026-07-14
Updated: 2026-10-15
Published by Unrust. Scoring and editorial method: https://unrust.dev/help#methodology

## Direct answer

Trace JavaScript by choosing one input and tracking the values, references, and callbacks that can change its output. Explicitly note whether an assignment creates a new object or another reference to the same object, and where promise or event callbacks observe state later. The resulting model gives you a useful prediction to compare with runtime behavior without pretending that manual tracing replaces a debugger.

## Original example

### Trace an alias before changing a selected list

```javascript
const selectedIds = ids;
selectedIds.push("new");

return ids.length;
```

The important question in this original example is not the final number alone. A useful trace marks selectedIds and ids as aliases of the same array, then asks whether mutation is allowed by the caller's contract. Replacing selectedIds with a copied array would change both the local result and the side effect visible to other code.

## Mark object identity as well as contents

When two JavaScript variables refer to the same array or object, a push, sort, or property assignment can surprise code that treats one name as a copy. Put an identity marker in your trace so you can distinguish a replacement from an in-place mutation and see which caller can observe it.



## Trace callback registration separately from execution

A callback's body is not necessarily executed where it appears. Note when it is registered, what variables it closes over, and what event or promise settlement invokes it. This prevents line-by-line reading from hiding an order-of-execution assumption.



## Check a concrete outcome

Use the original example with this input. These are local teaching fixtures, checked against the displayed snippet; they do not call external services.

Wrap the original return-based fragment in a function named example, keeping these inputs outside the function. The expression calls that function and observes its result.

The helper returns two and also mutates the caller's array because selectedIds and ids share one object.

### Input and local fixtures

```javascript
const ids = ["old"];
```

**Checked observation**

| Expression after the example | Observed result |
| --- | --- |
| ({returned: example(), ids}) | {"returned": 2, "ids": ["old", "new"]} |


- [JavaScript language reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference)

## Checklist

- Which JavaScript names refer to the same object or array?
- Where does a callback execute relative to the current stack?
- Which mutation can another caller observe after this function returns?

## Practice loop

1. Choose one JavaScript input and draw arrows for every shared object or array reference.
2. Record whether each meaningful statement replaces a value or mutates an existing one.
3. Predict when each callback runs, then check the sequence with a focused log or debugger.

## Limits of this page

Manual tracing becomes incomplete when browser events, concurrent requests, framework scheduling, or native APIs intervene. A short tracing attempt is a snapshot of one constrained problem and cannot determine why a particular execution model felt unfamiliar at that moment.

## Source context

Published by Unrust as practical teaching material using public language documentation and original examples. See the scoring and editorial method for scope, checking and limitations.

## Related resources

- [How to Trace State in Unfamiliar Code](https://unrust.dev/use-cases/trace-state-in-unfamiliar-code/index.md): Trace state in an unfamiliar function by following only the values, references, branches, and side effects that can change the behavior you are investigating.
- [How to Understand an AI-Generated Pull Request](https://unrust.dev/use-cases/understand-an-ai-generated-pull-request/index.md): Understand an AI-generated pull request by mapping the request to the diff, tracing its data flow, and isolating every assumption before approval.

Next step: [Take the free diagnostic](https://unrust.dev/diagnostic/index.md)
