State machine for JavaScript