<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-do-a-caesar-cipher-and-build-one-in-javascript--ceca4hzxd" -->

---
title: How to Do a Caesar Cipher (and Build One in JavaScript)
description: A Caesar cipher shifts every letter by a fixed amount and wraps from Z back to A, so shifting pizzapizzapizza by 2 gives rkbbcrkbbcrkbbc. This tutorial builds...
canonical: https://daily.dev/posts/how-to-do-a-caesar-cipher-and-build-one-in-javascript--ceca4hzxd
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How to Do a Caesar Cipher (and Build One in JavaScript) | daily.dev
og:description: A Caesar cipher shifts every letter by a fixed amount and wraps from Z back to A, so shifting pizzapizzapizza by 2 gives rkbbcrkbbcrkbbc. This tutorial builds...
og:url: https://daily.dev/posts/how-to-do-a-caesar-cipher-and-build-one-in-javascript--ceca4hzxd
og:image: https://api.daily.dev/og/posts/CeCa4HZXD.png
og:image:alt: How to Do a Caesar Cipher (and Build One in JavaScript)
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Do a Caesar Cipher (and Build One in JavaScript)

**[Kirupa](https://daily.dev/sources/kirupa)** · 38 min read · 0 upvotes · 0 comments

## Summary

A Caesar cipher shifts every letter by a fixed amount and wraps from Z back to A, so shifting pizzapizzapizza by 2 gives rkbbcrkbbcrkbbc. This tutorial builds an HTML, CSS and JavaScript app that encrypts as you type. Each letter is shifted using charCodeAt(0) and String.fromCharCode. The remainder operator (%) keeps any shift value in the 26-letter range. Decrypting uses the same shift with the opposite sign. The cipher is weak, since only 25 useful shifts exist, so anything secret needs modern encryption like the Web Crypto API.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.kirupa.com/html5/caesar_cipher.htm>

## Questions this post answers

### How do I implement a Caesar cipher in JavaScript with wraparound?

Loop through each letter, get its code with charCodeAt(0), add the shift, then wrap it back into the a to z range (97 to 122) by adding or subtracting 26. Turn the code back into a letter with String.fromCharCode. Applying shift % 26 first keeps any shift value, such as 200, inside the alphabet.

_Developers learning JavaScript string handling follow tutorials like this on daily.dev._

### How do I handle negative shifts and uppercase letters or digits in a JavaScript Caesar cipher?

Treat lowercase letters, uppercase letters and digits as separate ranges and wrap each with the remainder operator. Negative shifts need an extra step because -3 % 26 is -3 in JavaScript, not 23, so add the range size before taking the second %. Spaces and punctuation are returned unchanged.

_Edge cases like JavaScript's remainder behavior are the kind of detail daily.dev surfaces regularly._

### How do I decrypt a Caesar cipher and how secure is it?

If you know the shift used to encrypt, decrypt with the same number and the opposite sign, so -2 undoes a shift of 2. The cipher is not secure: with only 25 useful shift values, someone can try all of them in seconds. Use modern encryption such as the Web Crypto API for real secrets.

_Developers weighing basic ciphers against real encryption keep up with security fundamentals on daily.dev._

## Similar posts on daily.dev

- [Learn ciphers by breaking them - CipherLock](https://daily.dev/posts/learn-ciphers-by-breaking-them---cipherlock-ackahbc95) · Product Hunt · 0 upvotes · 0 comments
- [Typewriter Effect : Frontend Coding Exercises](https://daily.dev/posts/typewriter-effect-frontend-coding-exercises-zbpkrehxs) · Kirupa · 2 upvotes · 0 comments
- [Why Don’t Lowercase Letters Come Right After Uppercase Letters in ASCII? – Tyler Hillery](https://daily.dev/posts/why-don-t-lowercase-letters-come-right-after-uppercase-letters-in-ascii-tyler-hillery-dc3bph5od) · Lobsters · 0 upvotes · 0 comments
- [Creating 3D text engine in the browser](https://daily.dev/posts/creating-3d-text-engine-in-the-browser-9zfk5d8hf) · ITNEXT · 48 upvotes · 4 comments

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#css](https://daily.dev/tags/css), [#html](https://daily.dev/tags/html), [#cryptography](https://daily.dev/tags/cryptography)

[View this post on daily.dev](https://daily.dev/posts/how-to-do-a-caesar-cipher-and-build-one-in-javascript--ceca4hzxd)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to Do a Caesar Cipher (and Build One in JavaScript)","url":"https://daily.dev/posts/how-to-do-a-caesar-cipher-and-build-one-in-javascript--ceca4hzxd","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-do-a-caesar-cipher-and-build-one-in-javascript--ceca4hzxd"},"datePublished":"2026-11-28T06:30:37.579Z","dateModified":"2026-11-28T06:30:59.190Z","description":"A Caesar cipher shifts every letter by a fixed amount and wraps from Z back to A, so shifting pizzapizzapizza by 2 gives rkbbcrkbbcrkbbc. This tutorial builds...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/769dd1e7bd604752411bcf009752318b?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/769dd1e7bd604752411bcf009752318b?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Kirupa","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Kirupa","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/ff2fdb4927d34b81bd07cd18d6049a98","url":"https://daily.dev/sources/kirupa"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-to-do-a-caesar-cipher-and-build-one-in-javascript--ceca4hzxd","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"javascript,css,html,cryptography","timeRequired":"PT38M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Kirupa","item":"https://daily.dev/sources/kirupa"},{"@type":"ListItem","position":3,"name":"How to Do a Caesar Cipher (and Build One in JavaScript)"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-to-do-a-caesar-cipher-and-build-one-in-javascript--ceca4hzxd#faq","mainEntity":[{"@type":"Question","name":"How do I implement a Caesar cipher in JavaScript with wraparound?","acceptedAnswer":{"@type":"Answer","text":"Loop through each letter, get its code with charCodeAt(0), add the shift, then wrap it back into the a to z range (97 to 122) by adding or subtracting 26. Turn the code back into a letter with String.fromCharCode. Applying shift % 26 first keeps any shift value, such as 200, inside the alphabet. Developers learning JavaScript string handling follow tutorials like this on daily.dev."}},{"@type":"Question","name":"How do I handle negative shifts and uppercase letters or digits in a JavaScript Caesar cipher?","acceptedAnswer":{"@type":"Answer","text":"Treat lowercase letters, uppercase letters and digits as separate ranges and wrap each with the remainder operator. Negative shifts need an extra step because -3 % 26 is -3 in JavaScript, not 23, so add the range size before taking the second %. Spaces and punctuation are returned unchanged. Edge cases like JavaScript's remainder behavior are the kind of detail daily.dev surfaces regularly."}},{"@type":"Question","name":"How do I decrypt a Caesar cipher and how secure is it?","acceptedAnswer":{"@type":"Answer","text":"If you know the shift used to encrypt, decrypt with the same number and the opposite sign, so -2 undoes a shift of 2. The cipher is not secure: with only 25 useful shift values, someone can try all of them in seconds. Use modern encryption such as the Web Crypto API for real secrets. Developers weighing basic ciphers against real encryption keep up with security fundamentals on daily.dev."}}]}
```

