# speechSynthesis

> The `speechSynthesis` JS object controls the built-in text-to-speech engine of the browser.

## How do I make the browser speak text in JavaScript?

The object of the Web Speech API that speaks text aloud with voices installed on the device, without a server. Its `speak()`, `pause()` and `cancel()` methods control playback. Voices and quality differ between devices.

- Topic: Media & Audio
- Type: object
- Browser support: Chrome 33+, Chrome Android 33+, Edge 14+, Firefox 49+, Firefox Android 62+, Safari 7+, Safari iOS 7+

## Example

Wrong:

```js
const voices = speechSynthesis.getVoices();
button.addEventListener('click', () => {
  utterance.voice = voices[0];
  speechSynthesis.speak(utterance);
});
```

Right:

```js
let voices = speechSynthesis.getVoices();
speechSynthesis.addEventListener('voiceschanged', () => {
  voices = speechSynthesis.getVoices();
});
button.addEventListener('click', () => {
  utterance.voice = voices.find((v) => v.lang === 'en-US') ?? null;
  speechSynthesis.cancel();
  speechSynthesis.speak(utterance);
});
```

`getVoices()` can be empty at first, so refresh it on `voiceschanged`; `cancel()` stops old speech before new text.

## Common mistakes

- Assuming the same voice exists on every device.

## Key points

- The voice list is read after the `voiceschanged` event, because it can be empty at first.
- `cancel()` is called before speaking new text, so phrases do not pile up.

## Interview questions

### Why does `speechSynthesis.getVoices()` return an empty array?

Some browsers load voices asynchronously. Call `getVoices()` again in the `voiceschanged` event.

### How do I stop or pause speech?

Call `speechSynthesis.cancel()` to stop and clear the queue, or `pause()` and `resume()` to pause and continue.

### Does speechSynthesis work offline?

It depends on the voice. Voices with `localService` set to `true` run on the device; others may need a network connection.

## Related cards

- [SpeechSynthesisUtterance](https://wonderium.dev/js/speech-synthesis-utterance.md)
- [Text-to-Speech](https://wonderium.dev/ai/text-to-speech.md)
- [AudioContext](https://wonderium.dev/js/audio-context.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/speech-synthesis
- Last updated: 2026-11-25
