Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource


Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


  <h1>Javascript Calculator</h1>

<section class="calculator">
  <div id="screen"></div>
  <div id="keyboard">
    <div class="button number">1</div>
    <div class="button number">2</div>
    <div class="button number">3</div>
    <div class="button number">4</div>
    <div class="button number">5</div>
    <div class="button number">6</div>
    <div class="button number">7</div>
    <div class="button number">8</div>
    <div class="button number">9</div>
    <div id="clear" class="button">C</div>
    <div class="button number">0</div>
    <div id="comma" class="button operator">.</div>
    <div class="button operator">+</div>
    <div class="button operator">-</div>
    <div class="button operator">*</div>
    <div class="button operator">/</div>
    <div id="equal" class="button">=</div>

  <p>Created by <a href="" target="_blank">Rémy Beumier</a></p>


                * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  font-family: Arial;
  background: #D54F58;
  background-image: linear-gradient(to top left, #c0392b, #e74c3c , #9b59b6);
  text-align: center;

/* title */
header h1 {
  padding: 10px 5px;
  color: whitesmoke;
  letter-spacing: 0.2em;
  font-weight: normal;

/* main content */
.calculator {
  background: #2c3e50;
  width: 230px;
  margin: auto;
  border-radius: 2px;
  border: solid 5px whitesmoke;
  box-shadow: 3px 3px 3px 1px rgba(0,0,0,0.4);

.button, #screen {
  background: whitesmoke;
  color: #2c3e50;
  border-radius: 2px;

#screen {
  width: 200px;
  height: 45px;
  margin: 10px;
  text-align: right;
  font-size: 40px;
  color: #2c3e50;
  padding-right: 5px;
  box-shadow: inset 0 0 2px 1px rgba(0,0,0,0.4);

#keyboard {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-between;
  width: 200px;
  margin: 5px 10px;

.button {
  display: inline-block;
  box-shadow: 3px 3px 3px 1px black;
  color: #2c3e50;
  margin: 5px 0;
  cursor: pointer;
  font-size: 16px;
.button:active {
  box-shadow: none;

.number, #clear, #comma {
  width: 60px;
.operator {
  width: 95px;
#equal {
  width: 200px;
  font-size: 24px;

/* footer */
footer {
  font-size: 14px;
  padding: 5px;
  color: whitesmoke;
  text-align: center;
a {
  color: #2c3e50;
  text-decoration: none;
a:hover {
  color: whitesmoke;
  text-decoration: underline;



// add keypress events to put data

var operationElts = document.querySelectorAll(".number, .operator"),
    clearElt = document.querySelector("#clear"),
    equalElt = document.querySelector("#equal"),
    screenElt = document.querySelector("#screen");

// display numbers and operators clicked
for (let i=0; i<operationElts.length; i++) {
  operationElts[i].addEventListener("click", function() {
    screenElt.insertAdjacentHTML("beforeEnd", this.innerHTML);
// clear the calc
clearElt.addEventListener("click", function() {
  screenElt.innerHTML = "";
// process calc
equalElt.addEventListener("click", function() {
  // round answer to have max 6 decimals
  screenElt.innerHTML = Math.round(eval(screenElt.innerHTML) * 1000000) / 1000000;