Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Speech Synthesis in JavaScript

Speak typed text in JavaScript with SpeechSynthesisUtterance and speechSynthesis.speak. Cancel old speech first; the requested voice depends on the device.

BeginnerJavaScript Browser APIsExample 12 of 15

Save this as index.html and open it in a browser.

speech-synthesis.js
1<!doctype html>
2<html lang="en">
3<meta charset="utf-8">
4<title>JavaScript practice</title>
5<body>
6<label>Text <input id="words" value="Your study break starts now."></label><button id="speak">Speak</button><p id="status"></p>
7<script>
8const status = document.querySelector('#status');
9document.querySelector('#speak').onclick = () => {
10 if (!('speechSynthesis' in window)) { status.textContent = 'Speech is not supported.'; return; }
11 const text = document.querySelector('#words').value.trim();
12 if (!text) { status.textContent = 'Type a sentence first.'; return; }
13 speechSynthesis.cancel();
14 const message = new SpeechSynthesisUtterance(text);
15 message.lang = 'en-IN';
16 message.onend = () => { status.textContent = 'Finished speaking.'; };
17 message.onerror = () => { status.textContent = 'Speech could not finish.'; };
18 speechSynthesis.speak(message);
19 status.textContent = 'Speaking...';
20};
21</script>
22</body>
23</html>

Sample Output

Speaking...

The browser was asked to speak the typed message.

Finished speaking.

The utterance end event reported completion; the installed voice determines the sound.

In this one

  • Create a message to speak
  • Read text from an input
  • Clear an older speech queue
  • Respond to speech completion

How it works

  1. 1

    Check for speechSynthesis support after a click.

  2. 2

    Trim the input and reject an empty sentence.

  3. 3

    Cancel earlier queued speech.

  4. 4

    Create an utterance, set its language, and add end and error handlers.

  5. 5

    Speak the text; the status changes from Speaking to Finished speaking if it completes.

Edge cases

Empty input

A blank message has nothing useful to say.

Trim spaces and ask for a sentence instead of sending an empty utterance.

Voice unavailable

The chosen language may not have a matching voice.

The browser may choose another voice or fail. Test on the devices you plan to support and keep the visible text.

What's going on

Speech synthesis turns written text into spoken sound. It uses voices available through the browser or device. A study-break reminder can use it to read a short sentence aloud, but the text should remain visible for anyone who cannot hear it.

This browser-only page takes text from an input after a button click. A SpeechSynthesisUtterance is one message to speak. Setting lang requests an English voice for India; the available voice and pronunciation still depend on the device.

cancel clears older speech before a new message is queued. That prevents repeated clicks from building a long queue of reminders. The end and error events update the status when speech finishes or fails. The sample describes audible output, not console output. Do not rely on one exact voice or assume a requested language has an installed voice.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is an utterance here?

Pick an answer to check it.

Question 2

What does cancel do?

Pick an answer to check it.

Questions people ask

Does this record my microphone?

No. It converts typed text into speech. Speech recognition is a different feature.

Will every device sound the same?

No. Installed voices and browser speech engines differ.

Why cancel before speak?

Without cancellation, repeated clicks can queue the same message many times.