Menu

1 / 2

How to Build a Simple Morse Code Translator in JavaScript

DEV Community·morsetranslator·6 months ago
#q4VLkyqE
#coding#javascript#beginners#tutorial#morse
Reading 0:00
15s threshold

🚀 Introduction

Morse code is one of the oldest communication systems, but it’s still a fun and practical concept to explore—especially for developers. In this post, we’ll build a simple Morse code translator using JavaScript that can convert text into Morse code.

📡 What is Morse Code?

Morse code represents characters using dots (.) and dashes (-). Each letter and number has a unique pattern, which makes it possible to encode and decode messages.

🧠 Step 1: Create a Morse Code Map

First, define a mapping object:

const morseCodeMap = {

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: "----."

};

⚙️ Step 2: Convert Text to Morse Code

function textToMorse(text) {

return…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.