Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 30 / 31THE BROWSER

Interfaces reflect state.

A small state model prevents a page from showing contradictory messages.

01 / THE BASICS

Start here.

State is the data that describes what an interface currently knows. A counter's state might be the number 3; a search interface may have a query string, results, and a loading flag. Rendering means turning that state into visible page content. A useful rule is to decide what the screen should show from the current state, then update state when an event occurs and render the new result.

For a small page, this can be plain JavaScript: keep a variable count, write a render function that sets an element's textContent from count, and call render after count changes. The event handler does not need to remember every past click; it updates the state and lets rendering reflect the new value. More elaborate UI libraries organize the same relationship with their own rules.

A common source of confusion is keeping two sources of truth. If the variable says 3 but manually changed DOM text says 4, later code must guess which one is correct. Choose the state value as the source and render from it consistently. When the page shows a list, identify the data array, the rule that turns each item into a view, and the event that changes the array. This makes behavior easier to explain as the interface grows.

02 / THE IDEA

Build the model.

An interface is easier to understand when its visible output follows a small set of states. A form might be idle, sending, sent, or failed. If each state has a clear meaning, buttons, labels, and messages can change together rather than drifting apart.

Keep a single source of truth for each piece of information. Derived values can be calculated from state instead of stored again. Frameworks automate some rendering work, but the underlying design question is the same in plain DOM code: which facts can change, and what should the page show for each one?

03 / UNDER THE SURFACE

How it works.

State is the information that can change and affects what a user sees. For a form, status might be idle, sending, sent, or error. A rendering rule turns that state into a button label, disabled state, and feedback message. If each visible detail is derived from the same status, the interface is less likely to show contradictory information.

Avoid storing values you can calculate from other state. If items is your source list, count can be items.length rather than a second number you must update every time. When an event occurs, change the source state and then render the corresponding view. Frameworks can automate rerendering, but the mental model also works in plain DOM code: state first, display as a consequence.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
const status = 'sending';
const label = status === 'sending' ? 'Sending…' : 'Send';

First notice. The label is derived from status rather than maintained independently.

status is 'sending'. The conditional expression checks that string and chooses 'Sending…' for label. If status later becomes 'sent', calculating label again yields 'Send'. The code does not maintain a separate label variable by hand across every transition, which reduces the chance of stale text.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
const status = 'sending';
const buttonLabel = status === 'sending' ? 'Sending…' : 'Send';
const disabled = status === 'sending';
console.log(buttonLabel, disabled);

The output is 'Sending…' and true. Both values agree because they come from the same status. In a real handler, set status before starting the request, then set it to sent or error when the request finishes, and render after each change.

06 / KEEP THIS

One thing to remember.

Model the possible states before wiring up the visible controls.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.