State Machines for JavaScript Developers — How to Use Them in Your Apps

Learn how XState can benefit your application logic

Victoria Lo
StackAnatomy

--

What is a state machine?

A finite state machine is a mathematical model of computation, often used to design algorithms. It describes the behaviour of a system, which is defined in one single state at a time from a finite number of possible states.

For example, in a game, a character can have 4 possible states: idle, walk, run, and jump. The character cannot be in 2 states at the same time. The initial state is idle. From this state, the character can transition to walk, run or jump depending on the input received.

Once an input is received, only then the state can change and produce the corresponding output. Let’s say in this example, we choose to walk. When the state changes in response to our input, walk becomes our new current state and the character will show its walking animation as the output.

Why state machines?

At this point, you are probably wondering what are the advantages to implement state machines. The…

--

--

Victoria Lo
StackAnatomy

A nerd in books, tea and programming. I publish weekly on my personal blog: https://lo-victoria.com/