Morse Code Translator: how it works and how to build one
Translate text to Morse code and Morse back to text. This guide explains what happens behind the button and shows the code, so you can build your own.
What it does
Letters, numbers and common punctuation are converted using International Morse code. Letters are separated by spaces and words by a slash. Paste Morse with dots and dashes to read it.
When to use it
- Puzzles and escape rooms
- Learning Morse
- Fun messages
How to use it
- Choose Text → Morse or Morse → Text.
- Type or paste.
- Copy the result.
Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/morse-code
How it works
Morse code maps each letter and digit to a pattern of dots and dashes. The translator is a lookup table: split the text into words and letters, look each one up, and join letters with spaces and words with a slash.
Decoding reverses the table and splits on slashes and spaces.
Build it yourself
Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.
morse.ts (ts)
const MORSE: Record<string, string> = { A: ".-", B: "-...", C: "-.-.", D: "-..", E: ".", S: "...", O: "---", 1: ".----" /* … */ };
const BACK = Object.fromEntries(Object.entries(MORSE).map(([k, v]) => [v, k]));
export const toMorse = (s: string) =>
s.toUpperCase().trim().split(/\s+/)
.map((w) => [...w].map((c) => MORSE[c]).filter(Boolean).join(" "))
.join(" / ");
export const fromMorse = (s: string) =>
s.trim().split(/\s*\/\s*/).map((w) => w.split(" ").map((c) => BACK[c] ?? "?").join("")).join(" ");
Make it production-ready
- Play the code as sound with the Web Audio API.
- Add a flashing light mode.
Questions
How are words separated?
With a slash ( / ). Three or more spaces also work when decoding.
Free, runs in your browser, nothing is uploaded.