Home Blog Work

Speech to text using javascript

Published on November 25, 2016

Speech recognizing software everywhere nowadays. From Google’s latest Assistance to Apple’s Siri, Amazon’s Echo and so many out there. Almost all big Sofware development companies have their Own AI implementation. This is like Browsers war 10 years back but this time AI war begins…

Anyway, Today we’ll create a simple web application to which we can shout at. It will recognize what we speak, Convert it into text and show in a textbox. Isn’t it cool?


In order to do this, we will use [SpeechRecognition]( API.

This technology is still new. All browser may not support this. Some browser may support with a browser prefix.

Ok, now let’s create our index.html.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8" />
    <title>Speech To Text</title>
    <link rel="stylesheet" href="./app.css" />
    <form role="form">
      <legend>Speech to text</legend>
      <em id="speek-now">Speak now...</em>
      <div class="form-group">
        <textarea type="text" class="form-control" id="editer" rows="10">
      <button type="button" class="btn btn-primary" id="again">
        Click Here to Speek
    <script src="./app.js"></script>

Simple form with textarea and a button element.

Let’s see the app.js

(function() {
  var SpeechRecognition =
    SpeechRecognition ||
    webkitSpeechRecognition ||
    mozSpeechRecognition ||

  // End if SpeechRecognition api not available in Browser
  if (!SpeechRecognition) {
    alert("Your Browser dosen't support");

  var speech = new SpeechRecognition();
  speech.lang = "en-US";

  // if any error occour
  speech.onerror = function(event) {
    if (event.error == "not-allowed") {
      alert("Please allow microphone.");
    } else {
      alert("There is an error. Please see your console");

  // on result event;
  speech.onresult = function(event) {
    document.querySelector("#editer").value = event.results[0][0].transcript;

  //on speak button click
  document.querySelector("#speek").addEventListener("click", function(e) {

  var nowflg = true;
  function toggle() {
    document.querySelector("#speek-now").style.visibility = nowflg
      ? "hidden"
      : "visible";
    nowflg = !nowflg;

In the first line, we try to get the SpeechRecognition function.

There are following events available for the SpeechRecognition API.


For this tutorial, we will use only onresult and onerror event callbacks.

In onerror callback we will handle with user permission to the browser.

In onresult callback we will update the textbox with speech transcript text.

Live demo here.