Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Converting a string to NATO phonetic alphabets in JavaScript
The NATO phonetic alphabet is a standardized set of code words used to represent letters of the alphabet in radio and telephone communications. This tutorial shows how to convert any string into NATO phonetic alphabet representation using JavaScript.
NATO Phonetic Alphabet
The 26 code words are: Alfa, Bravo, Charlie, Delta, Echo, Foxtrot, Golf, Hotel, India, Juliett, Kilo, Lima, Mike, November, Oscar, Papa, Quebec, Romeo, Sierra, Tango, Uniform, Victor, Whiskey, X-ray, Yankee, Zulu.
Implementation
Here's a complete function that converts a string to NATO phonetic alphabet:
const str = 'this is simple string';
const convertToNato = (str = '') => {
let nato = {
a: 'Alfa',
b: 'Bravo',
c: 'Charlie',
d: 'Delta',
e: 'Echo',
f: 'Foxtrot',
g: 'Golf',
h: 'Hotel',
i: 'India',
j: 'Juliett',
k: 'Kilo',
l: 'Lima',
m: 'Mike',
n: 'November',
o: 'Oscar',
p: 'Papa',
q: 'Quebec',
r: 'Romeo',
s: 'Sierra',
t: 'Tango',
u: 'Uniform',
v: 'Victor',
w: 'Whiskey',
x: 'X-ray',
y: 'Yankee',
z: 'Zulu'
};
let arr = [...str];
return arr
.filter((letter) => letter !== " ")
.map((letter) => {
if (/[^a-z]/.test(letter.toLowerCase())) {
return letter;
} else {
return nato[letter.toLowerCase()];
}
}).join(' ');
};
console.log(convertToNato(str));
Tango Hotel India Sierra India Sierra Sierra India Mike Papa Lima Echo Sierra Tango Romeo India November Golf
How It Works
The function works in several steps:
- Creates a mapping object with letters as keys and NATO code words as values
- Converts the string to an array using spread operator
[...str] - Filters out spaces to focus only on letters and other characters
- Maps each letter to its NATO equivalent or keeps non-alphabetic characters unchanged
- Joins the result with spaces
Enhanced Version with Space Handling
Here's an improved version that preserves word boundaries:
const convertToNatoWithSpaces = (str = '') => {
const nato = {
a: 'Alfa', b: 'Bravo', c: 'Charlie', d: 'Delta', e: 'Echo',
f: 'Foxtrot', g: 'Golf', h: 'Hotel', i: 'India', j: 'Juliett',
k: 'Kilo', l: 'Lima', m: 'Mike', n: 'November', o: 'Oscar',
p: 'Papa', q: 'Quebec', r: 'Romeo', s: 'Sierra', t: 'Tango',
u: 'Uniform', v: 'Victor', w: 'Whiskey', x: 'X-ray', y: 'Yankee', z: 'Zulu'
};
return str.toLowerCase()
.split('')
.map(char => {
if (char === ' ') return '|'; // Word separator
if (/[a-z]/.test(char)) return nato[char];
return char; // Numbers, punctuation, etc.
})
.join(' ');
};
console.log(convertToNatoWithSpaces('hello world 123'));
Hotel Echo Lima Lima Oscar | Whiskey Oscar Romeo Lima Delta 1 2 3
Example with Mixed Characters
const testString = 'ABC-123';
console.log('Original:', testString);
console.log('NATO:', convertToNato(testString));
Original: ABC-123 NATO: Alfa Bravo Charlie - 1 2 3
Conclusion
Converting strings to NATO phonetic alphabet is useful for clear communication in radio transmissions and telecommunications. The function handles both letters and non-alphabetic characters appropriately.
