Converting string to MORSE code in JavaScript

What is Morse Code?

Morse code is a method used in telecommunications to encode text characters as standardized sequences of two different signal durations, called dots and dashes. Each letter of the alphabet has a unique pattern of dots (.) and dashes (-).

To convert a string to Morse code in JavaScript, we need an object that maps all alphabets to their Morse code equivalents, then iterate through the input string to build the encoded result.

Morse Code Map

First, let's create an object containing all alphabet-to-Morse mappings:

const morseCode = {
    "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": "--.."
};

console.log(morseCode["A"]); // Display Morse for 'A'
console.log(morseCode["S"]); // Display Morse for 'S'
.-
...

Basic Conversion Function

Here's a function that converts any string to Morse code:

const morseCode = {
    "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": "--.."
};

const convertToMorse = (str) => {
    return str.toUpperCase().split("").map(char => {
        return morseCode[char] ? morseCode[char] : char;
    }).join(" ");
};

console.log(convertToMorse("HELLO"));
console.log(convertToMorse("SOS"));
.... . .-.. .-.. ---
... --- ...

Enhanced Version with Space Handling

Let's improve the function to handle spaces and special characters better:

const morseCode = {
    "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": "--..", " ": "/"
};

const convertToMorseEnhanced = (str) => {
    return str.toUpperCase().split("").map(char => {
        if (morseCode[char]) {
            return morseCode[char];
        } else if (char === " ") {
            return "/"; // Space separator
        } else {
            return char; // Keep special characters as-is
        }
    }).join(" ");
};

console.log(convertToMorseEnhanced("HELLO WORLD"));
console.log(convertToMorseEnhanced("HI THERE!"));
.... . .-.. .-.. --- / .-- --- .-. .-.. -..
.... .. / - .... . .-. . !

Complete Example

Here's a comprehensive example demonstrating different input strings:

const morseCode = {
    "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": "--.."
};

const stringToMorse = (text) => {
    return text.toUpperCase()
               .split("")
               .map(char => morseCode[char] || char)
               .join(" ");
};

// Test different inputs
console.log("Original: JAVASCRIPT");
console.log("Morse:", stringToMorse("JAVASCRIPT"));
console.log();
console.log("Original: MORSE CODE");  
console.log("Morse:", stringToMorse("MORSE CODE"));
Original: JAVASCRIPT
Morse: .--- .- ...- .- ... -.-. .-. .. .--. -

Original: MORSE CODE
Morse: -- --- .-. ... .   -.-. --- -.. .

Conclusion

Converting strings to Morse code in JavaScript is straightforward using object mapping and string methods. The key is creating a lookup object for character-to-Morse translation and handling spaces and special characters appropriately.

Updated on: 2026-03-15T23:18:59+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements