A library for finite state machines.

VERSION 3.0 Is a significant rewrite from earlier versions. Existing 2.x users should be sure to read the Upgrade Guide.
In a browser:
<script src='state-machine.js'></script>
after downloading the source or the minified version
Using npm:
npm install --save-dev javascript-state-machine
In Node.js:
var StateMachine = require('javascript-state-machine');
A state machine can be constructed using:
var fsm = new StateMachine({
init: 'solid',
transitions: [
{ name: 'melt', from: 'solid', to: 'liquid' },
{ name: 'freeze', from: 'liquid', to: 'solid' },
{ name: 'vaporize', from: 'liquid', to: 'gas' },
{ name: 'condense', from: 'gas', to: 'liquid' }
],
methods: {
onMelt: function() { console.log('I melted') },
onFreeze: function() { console.log('I froze') },
onVaporize: function() { console.log('I vaporized') },
onCondense: function() { console.log('I condensed') }
}
});
... which creates an object with a current state property:
fsm.state... methods to transition to a different state:
fsm.melt()fsm.freeze()fsm.vaporize()fsm.condense()... observer methods called automatically during the lifecycle of a transition:
onMelt()onFreeze()onVaporize()onCondense()... along with the following helper methods:
fsm.is(s) - return true if state s is the current statefsm.can(t) - return true if transition t can occur from the current statefsm.cannot(t) - return true if transition t cannot occur from the current statefsm.transitions() - return list of transitions that are allowed from the current statefsm.allTransitions() - return list of all possible transitionsfsm.allStates() - return list of all possible statesA state machine consists of a set of States
A state machine changes state by using Transitions
A state machine can perform actions during a transition by observing Lifecycle Events
A state machine can also have arbitrary Data and Methods.
Multiple instances of a state machine can be created using a State Machine Factory.
Read more about
You can Contribute to this project with issues or pull requests.
See RELEASE NOTES file.
See MIT LICENSE file.
If you have any ideas, feedback, requests or bug reports, you can reach me at jakesgordon@gmail.com, or via my website: jakesgordon.com
Overall
Maintained
Code review
Latest release
Releases this year
(top 24 of 39)
318 followers · starred Mar 2013
465 followers · starred Dec 2021
294 followers · starred Apr 2018
237 followers · starred Mar 2025
A library for finite state machines.

VERSION 3.0 Is a significant rewrite from earlier versions. Existing 2.x users should be sure to read the Upgrade Guide.
In a browser:
<script src='state-machine.js'></script>
after downloading the source or the minified version
Using npm:
npm install --save-dev javascript-state-machine
In Node.js:
var StateMachine = require('javascript-state-machine');
A state machine can be constructed using:
var fsm = new StateMachine({
init: 'solid',
transitions: [
{ name: 'melt', from: 'solid', to: 'liquid' },
{ name: 'freeze', from: 'liquid', to: 'solid' },
{ name: 'vaporize', from: 'liquid', to: 'gas' },
{ name: 'condense', from: 'gas', to: 'liquid' }
],
methods: {
onMelt: function() { console.log('I melted') },
onFreeze: function() { console.log('I froze') },
onVaporize: function() { console.log('I vaporized') },
onCondense: function() { console.log('I condensed') }
}
});
... which creates an object with a current state property:
fsm.state... methods to transition to a different state:
fsm.melt()fsm.freeze()fsm.vaporize()fsm.condense()... observer methods called automatically during the lifecycle of a transition:
onMelt()onFreeze()onVaporize()onCondense()... along with the following helper methods:
fsm.is(s) - return true if state s is the current statefsm.can(t) - return true if transition t can occur from the current statefsm.cannot(t) - return true if transition t cannot occur from the current statefsm.transitions() - return list of transitions that are allowed from the current statefsm.allTransitions() - return list of all possible transitionsfsm.allStates() - return list of all possible statesA state machine consists of a set of States
A state machine changes state by using Transitions
A state machine can perform actions during a transition by observing Lifecycle Events
A state machine can also have arbitrary Data and Methods.
Multiple instances of a state machine can be created using a State Machine Factory.
Read more about
You can Contribute to this project with issues or pull requests.
See RELEASE NOTES file.
See MIT LICENSE file.
If you have any ideas, feedback, requests or bug reports, you can reach me at jakesgordon@gmail.com, or via my website: jakesgordon.com
Overall
Maintained
Code review
Latest release
Releases this year
(top 24 of 39)
318 followers · starred Mar 2013
465 followers · starred Dec 2021
294 followers · starred Apr 2018
237 followers · starred Mar 2025