HOWTO · JavaScript
Caesar Cipher in JavaScript
In this article we are going to learn what is the Caesar Cipher in JavaScript and what is the purpose and benefits of this feature in JavaScript source code with different examples.
On this page
In JavaScript, a Caesar cipher replaces each English letter with another letter a fixed number of places away. The implementation below encrypts and decrypts text with an integer shift. It is useful for learning a substitution cipher, not for protecting real data.
Caesar Cipher Basics
The cipher uses the 26 letters of the English alphabet. With a shift of 3, A becomes D, B becomes E, and X becomes A after the alphabet wraps around. Applying the same shift in reverse restores the original letters in lowercase; it does not preserve their original case.
How the Caesar Cipher Works
Process each character and shift it only when it is an English letter. The implementation converts ASCII uppercase letters to lowercase, so Hello World is treated as hello world; spaces, punctuation, digits, and characters outside the English alphabet pass through unchanged. For a shift of 3, hello world maps to khoor zruog.
You can describe the mapping with letter indexes: a is 0, b is 1, and z is 25. For each English letter, add the shift to its index and take the remainder after division by 26. That remainder wraps values past z back to the start. Decryption subtracts the shift; the implementation uses the equivalent reverse shift so the same mapping code serves both operations.
The shift can be any integer that JavaScript represents precisely. Reducing it modulo 26 makes values such as 29 equivalent to 3, while a shift of -1 moves each letter one place backward. Since there are 26 letters, a shift of 26 leaves letters unchanged. The code normalizes the remainder because JavaScript’s % operator can produce a negative result for a negative shift.
Implement Caesar Cipher in JavaScript
The shared shiftText function handles both directions. encrypt applies the requested shift, and decrypt applies its reverse. Each function defaults to a shift of 3; pass another integer to change it. The implementation expects a string and an integer shift.
Run this example:
function shiftText(text, shift) {
const amount = ((shift % 26) + 26) % 26;
return text.split('').map((char) => {
let code = char.charCodeAt(0);
if (code >= 65 && code <= 90) code += 32;
if (code < 97 || code > 122) return char;
return String.fromCharCode(((code - 97 + amount) % 26) + 97);
}).join('');
}
function encrypt(text, shift = 3) {
return shiftText(text, shift);
}
function decrypt(text, shift = 3) {
return shiftText(text, -shift);
}
console.log(encrypt('hello world', 3));
console.log(decrypt('khoor zruog', 3));
khoor zruog
hello world
The first call encrypts hello world, and the second decrypts that result. The output shows the encrypted text followed by its decrypted form. The modulo calculation keeps the shift within the alphabet even when the supplied integer is larger than 25 or less than 0. The split('') call makes an array of string units for the mapping step, and join('') combines the mapped units into a result. For more detail on splitting strings into characters, see JavaScript String split().
Because the mapping covers only the English alphabet, accented letters and symbols are left as they are, while ASCII uppercase letters are emitted in lowercase. If an application needs a different alphabet, define its letters and case rules explicitly and adapt the mapping rather than assuming this function handles every writing system. Pass an integer shift; fractional shifts do not represent a meaningful number of alphabet positions.
Limitations and Security
A Caesar cipher has only 26 possible shifts, so it is easy to break by trying them. It also reveals patterns in the original text and is not designed to protect messages from anyone who can read them. Do not use it to secure server requests, database contents, passwords, or other sensitive information. Use established cryptographic tools and protocols for security-sensitive applications.