Morse Code in JavaScript: Encoder & Decoder Guide
Build a Morse code encoder and decoder in JavaScript. Includes a full character map, text-to-Morse and Morse-to-text functions, and Web Audio API playback.
Morse code encodes text as sequences of dots (short signals) and dashes (long signals). Each letter and digit maps to a unique dot-dash pattern. Implementing a Morse code encoder and decoder in JavaScript is a practical exercise in hash map lookups, string manipulation, and audio synthesis (if you want to play the beeps). This guide covers a complete, working implementation you can drop into any project.
How to Use the Morse Code Converter Tool
- Enter text — Type a message in the text field. The tool encodes it to Morse code instantly.
- Or enter Morse code — Paste dots and dashes (separated by spaces for letters, slashes for words) to decode back to text.
- Copy the result — Use the copy button to grab the encoded or decoded output.
- Use the code below — Implement the same encoder/decoder in your own JavaScript application.
Morse Code Encoder in JavaScript
const MORSE_CODE = {
A: '.-', B: '-...', C: '-.-.', D: '-..', E: '.',
F: '..-.', G: '--.', H: '....', I: '..', J: '.---',
K: '-.-', L: '.-..', M: '--', N: '-.', O: '---',
P: '.--.', Q: '--.-', R: '.-.', S: '...', T: '-',
U: '..-', V: '...-', W: '.--', X: '-..-', Y: '-.--',
Z: '--..',
'0': '-----', '1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....', '7': '--...',
'8': '---..', '9': '----.'
};
function textToMorse(text) {
return text
.toUpperCase()
.split(' ')
.map(word =>
word.split('').map(char => MORSE_CODE[char] || '?').join(' ')
)
.join(' / ');
}
console.log(textToMorse('HELLO WORLD'));
// .... . .-.. .-.. --- / .-- --- .-. .-.. -..
Morse Code Decoder in JavaScript
const REVERSE_MORSE = Object.fromEntries(
Object.entries(MORSE_CODE).map(([k, v]) => [v, k])
);
function morseToText(morse) {
return morse
.split(' / ')
.map(word =>
word.split(' ').map(code => REVERSE_MORSE[code] || '?').join('')
)
.join(' ');
}
console.log(morseToText('.... . .-.. .-.. --- / .-- --- .-. .-.. -..'));
// HELLO WORLD
Playing Morse Code Audio in the Browser
async function playMorse(morse, wpm = 20) {
const ctx = new AudioContext();
const dotDuration = 1200 / wpm / 1000; // seconds
let time = ctx.currentTime;
for (const symbol of morse) {
if (symbol === '.') {
playTone(ctx, time, dotDuration);
time += dotDuration * 2;
} else if (symbol === '-') {
playTone(ctx, time, dotDuration * 3);
time += dotDuration * 4;
} else if (symbol === ' ') {
time += dotDuration * 3;
} else if (symbol === '/') {
time += dotDuration * 7;
}
}
}
function playTone(ctx, startTime, duration) {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.connect(gain);
gain.connect(ctx.destination);
osc.frequency.value = 600; // Hz
osc.start(startTime);
osc.stop(startTime + duration);
}
Why Implement Morse Code in JavaScript?
- Educational projects — Morse code converters are excellent practice for hash map lookups, string splitting, and array mapping.
- Accessibility tools — Morse code can be an input method for users with motor impairments, using single-switch devices.
- Audio feedback UIs — The Web Audio API integration lets you play audible Morse signals directly in the browser.
- Encoding challenges — Morse code is a good starting point for understanding encoding systems, substitution ciphers, and data transformation.
Test your Morse code conversions with the Morse Code Converter — encode and decode text instantly.