Today, in this post we will be discussing how to create a calculator in javascript for free. In the last post, we discussed how to create a beautiful countdown timer in HTML, CSS, and javascript. Let’s see, how to create a calculator in javascript.
Calculator in Javascript:
This calculator is a simple calculator and it is designed using CSS properties. It’s also working.
Html:
As HTML is a markup language and we use this language to create the web page skeleton. We can only create the skeleton of the calculator in HTML but we can’t design it using Html.
CSS properties:
CSS has a huge list of properties and we can use these properties for free to design our website pages. We have designed the screen and buttons of the calculator with the help of CSS properties. This calculator has a soft UI.
You might like it:
- Flip page in HTML, css and javascript
- Shuffle cards using CSS and javascript
- 3D carousel Html CSS JavaScript
Javascript libraries:
Javascript has a huge list of libraries. It is used for various purposes. We have used the javascript libraries just to add the functionalities to our calculator buttons and switch from dark mode to light mode. Calculator cant work without javascript libraries. This is a basic calculator. So, if you want to create this type of simple calculator in javascript then follow us.
How to create a calculator in javascript:
Follow below steps
Download a code editor:
It is the basic need for creating this type of project in HTML, CSS, and javascript.
Html source code:
Now save the file by using the extension .html at the end of the file name. This extension will understand the browser and code editor to understand the type of file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Calculator UI - FantacyDesigns </title> <link href='https://unpkg.com/boxicons@2.1.2/css/boxicons.min.css' rel='stylesheet'> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"> <link rel="stylesheet" href="assets/css.css"> </head> <body> <div class="container"> <div class="iphone-x"> <div class="screen"> <div class="notch"> <div class="sound"></div> <div class="camera"></div> </div> <div class="time" id="time"> <script> var time = document.getElementById('time').innerHTML; var today = new Date(); var time = today.getHours() + ":" + today.getMinutes(); time = document.write(time); </script> </div> <div class="battery"> <i class="fa fa-battery-full"></i> </div> <div class="wifi"> <i class="fa fa-wifi"></i> </div> <div class="top-nav"></div> <form class="calculator container"> <input type="text" placeholder="0" name="displayResult" class="area" /> <div class="key container" id="keyBoard"> <div class="first-col"></div> <div class="second-col"> <div class="button"> <button type="button" name="" value="AC" class="btn-work" onclick="displayResult.value=' '" id="c"> AC </button> </div> <div class="button"> <button type="button" name="" value="7" onclick="calcNumbers(this.value)" id="num"> 7 </button> </div> <div class="button"> <button type="button" name="" value="4" onclick="calcNumbers(this.value)" id="num"> 4 </button> </div> <div class="button"> <button type="button" name="" value="1" onclick="calcNumbers(this.value)" id="num"> 1 </button> </div> <div class="button"> <button type="button" name="" value="0" onclick="calcNumbers(this.value)" id="num"> 0 </button> </div> </div> <div class="third-col"> <div class="button"> <button type="button" name="" value="+/-" onclick="makeNegative(displayResult.value)" class="btn-work" id="c"> +/- </button> </div> <div class="button"> <button type="button" name="" value="8" onclick="calcNumbers(this.value)" id="num"> 8 </button> </div> <div class="button"> <button type="button" name="" value="5" onclick="calcNumbers(this.value)" id="num"> 5 </button> </div> <div class="button"> <button type="button" name="" value="2" onclick="calcNumbers(this.value)" id="num"> 2 </button> </div> <div class="button"> <button type="button" name="" value="." onclick="calcNumbers(this.value)" class="btn-work" id="num"> . </button> </div> </div> <div class="fourth-col"> <div class="button"> <button type="button" name="" value="%" onclick="calcNumbers(this.value)" class="btn-work" id="c"> <i class="fa fa-percent"></i> </button> </div> <div class="button"> <button type="button" name="" value="9" onclick="calcNumbers(this.value)" id="num"> 9 </button> </div> <div class="button"> <button type="button" name="" value="6" onclick="calcNumbers(this.value)" id="num"> 6 </button> </div> <div class="button"> <button type="button" name="" value="3" onclick="calcNumbers(this.value)" id="num"> 3 </button> </div> <div class="button"> <button type="button" name="" value="+" onclick="calcNumbers(this.value)" class="btn-work" id="num"> <i class="fa fa-plus"></i> </button> </div> </div> <div class="fifth-col"> <div class="button"> <button type="button" name="" value="/" onclick="calcNumbers(this.value)" class="btn-work" id="eql"><i class="fa fa-divide"></i> </button> </div> <div class="button"> <button type="button" name="" value="*" onclick="calcNumbers(this.value)" class="btn-work" id="eql"><i class="fa fa-times"></i> </button> </div> <div class="button"> <button type="button" name="" value="-" onclick="calcNumbers(this.value)" class="btn-work" id="eql"><i class="fa fa-minus"></i> </button> </div> <div class="button"> <button type="button" name="" value="=" onclick="(displayResult.value.includes('%'))?percentage(displayResult.value):displayResult.value=eval(displayResult.value);" class="btn-equal" id="eql"> = </button> </div> </div> <div class="menu"><i class='bx bx-menu-alt-left'></i></div> <div class="home"><i class='bx bx-home-alt-2'></i></div> <div class="back"><i class='bx bx-arrow-back'></i></div> </div> </form> </div> </div> </div> <div class="drak-light"> <i class="bx bx-moon moon"></i> <i class="bx bx-sun sun"></i> </div> <script src="assets/js.js"></script> </body> </html> |
CSS source code:
Again create a new file for CSS. The compiler and code editor will understand the type of the file by using the extension of .css at the end of the file name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap'); :root { --bodybg:#e7e8ee; --btt:#222222; --tcolor: rgb(119 119 119); --BxSi: 6px 6px 8px rgba(13, 39, 80, 0.25), -6px -6px 10px #fff, inset -8px -8px 12px rgba(255,255,255,0.7), inset 5px 5px 8px rgba(13,39,80,0.2); --PSD-3d: 7px 7px 15px #45566754, -8px -8px 15px #5165790a, inset -5px -4px 13px 0px #51657959, inset 6px 7px 12px 0px #fffffff0; --eql-background: linear-gradient(to bottom right, rgb(211 109 18),rgb(237 157 30)); --eql-shadow: inset 1px 1px 3px rgb(249 133 0), inset 4px 4px 15px rgb(179 83 0), inset -1px -1px 3px rgb(204 99 8), 3px 3px 5px rgb(194 199 212); --c-background: linear-gradient(to bottom right, rgb(139 148 161),rgb(239 240 247)); --c-shadow: inset 1px 1px 1px rgb(200 206 217), inset 2px 2px 8px rgb(202 207 214), inset -2px -2px 3px rgb(180 187 199), 3px 3px 12px rgb(194 199 212); --num-background: linear-gradient(to bottom right, rgb(192 197 209),rgb(237 238 245)); --num-shadow: inset 2px 2px 3px rgb(235 235 243), inset 4px 4px 15px rgb(202 207 214), 3px 3px 12px rgb(194 199 212); } body.dark{ --bodybg:#1e212a; --btt:#fff; --tcolor: rgb(119 119 119); --BxSi:6px 6px 8px rgb(0 0 0 / 50%), -6px -6px 10px rgb(255 255 255 / 5%), inset -8px -8px 12px rgb(255 255 255 / 5%), inset 5px 5px 8px rgb(0 0 0 / 50%); --PSD-3d: 5px 5px 15px rgb(0 0 0 / 18%), 5px 15px 15px rgb(0 0 0 / 25%), inset 5px 5px 10px rgb(0 0 0 / 50%), inset 5px 5px 20px rgb(255 255 255 / 20%), inset -5px -5px 15px rgb(0 0 0 / 75%); --eql-shadow: inset 1px 1px 2px rgb(249 170 45 / 60%), inset 4px 4px 15px rgb(165 78 4), 3px 3px 5px rgb(24 27 36); --eql-shadow: inset 1px 1px 2px rgb(249 170 45 / 60%), inset 4px 4px 15px rgb(165 78 4), 3px 3px 5px rgb(24 27 36); --c-background: linear-gradient(to bottom right, rgb(73 79 92),rgb(111 117 135)); --c-shadow: inset 1px 1px 1px rgb(101 107 123), inset 4px 4px 15px rgb(51 57 66), 3px 3px 5px rgb(24 27 36); --num-background: linear-gradient(to bottom right, rgb(41 45 56),rgb(60 60 68)); --num-shadow: inset 1px 1px 1px rgb(57 60 74), inset 4px 4px 15px rgb(32 35 45), 3px 3px 5px rgb(24 27 36); } /*** ***/ * { margin: 0; padding: 0; font-family: 'Roboto', sans-serif; } body { background: var(--bodybg); } .iphone-x { margin-top: 100px; } .iphone-x .screen { width: 42%; height: 640px; margin: 0 auto; min-width: 310px; max-width: 310px; border-radius: 44px; background: var(--bodybg); padding: 10px; box-shadow:var(--PSD-3d); } .screen .notch { width: 159px; height: 30px; margin: 0 auto; /** background: red;**/ border-bottom-right-radius: 15px; border-bottom-left-radius: 15px; } .sound { top: 10px; left: 54px; width: 40px; height: 6px; position: relative; background: var(--btt); border-radius: 10px; } .camera { top: 2px; left: 110px; width: 10px; height: 10px; position: relative; background: var(--btt); border-radius: 100px; } .time { color: var(--btt); width: 60px; font-size: 11px; margin-top: -22px; background: transparent; margin-left: 18px; } .battery { margin-top: -20px; } .battery i { color: var(--btt); margin-left: 230px !important; } .wifi { margin-top: -18px !important; margin-left: 260px; } .wifi i { color: var(--btt); margin-top: -20px; } .area { min-width: 50% !important; width: 94% !important; min-height: 120px !important; margin-left: 6px; margin-top: -5px; font-size: 30px !important; text-align: right; overflow: scroll; outline: none; border: none; font-weight: 400; color: var(--btt); background: var(--bodybg); border-radius: 10px; padding: 2px; box-shadow: var(--BxSi); } .calculator { margin: 0; margin-top: 38px; box-sizing: content-box; } .key { float: left !important; font-size: 15px; display: flex !important; margin: -5%; margin-left: auto !important; margin-right: auto !important; } .fifth-col, .fourth-col, .third-col, .second-col, .first-col { padding-left: 9px; display: inline-block; margin-top: 20px !important; justify-content: space-between !important; } .button { display: block; padding-left: 40px; min-height: 40px; padding-right: 30px; white-space: wrap; word-wrap: break-word; margin-top: 20px; justify-content: space-between !important; } button { position: relative; margin-left: -54px !important; min-width: 55px; align-items: center; justify-content: center; text-align: center; padding: 0px; color: var(--btt) !important; border: none; border-radius: 17px !important; height: 55px; outline: none; font-weight: 400 !important; font-size: 14pt !important; } .btn-work { color: var(--btt) !important; } .btn-equal { color: var(--btt) !important; height: 120px !important; } button:focus { cursor: pointer; outline: none !important; border: none !important; } button:active { outline: none !important; border: none !important; } button:hover { color: #696a6a !important; } .btn-work:hover, .btn-equal:hover { color: #0b254e !important; } .home { margin-top: 415px; margin-left: 66px; font-size: 30px; cursor: pointer; color:var(--btt); } .menu { margin-top: 422px; margin-left: -280px; font-size: 20px; cursor: pointer; color:var(--btt); } .back { margin-top: 422px; margin-left: 75px; font-size: 20px; cursor: pointer; color:var(--btt); } #eql { background: var(--eql-background); box-shadow: var(--eql-shadow); color: #fff !important; } #c { background: var(--c-background); box-shadow: var(--c-shadow); } #num { background: var(--num-background); box-shadow: var(--num-shadow); } .drak-light { left: 50px; display: flex; position: absolute; justify-content: center; top: 50px; width: 60px; height: 60px; background: var(--num-background); box-shadow: var(--num-shadow); text-align: center; align-items: center; border-radius: 50%; } .drak-light i { position: absolute; font-size: 25px; cursor: pointer; transition: all 0.3s ease; } .drak-light i.sun { opacity: 0; pointer-events: none; } .drak-light.active i.sun{ opacity: 1; pointer-events:auto; } .drak-light.active i.moon{ opacity: 0; pointer-events: none; } .bx-sun { color: #ffd900; } .bx-moon { color: #888; } |
Javascript source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
function calcNumbers(result) { document.querySelector('.area').value += result; } function percentage(result) { var result1 = String(result); if (result1.includes("%")) { var a, b, percent; var splitWord = []; a = Number(splitWord[0]); b = Number(splitWord[1]); percent = (a / 100 * b); document.querySelector('.area').value = percent; } } function makeNegative(result) { if (result.charAt(0) == '_') { document.querySelector('.area').value = result.replace('-', ''); } else { document.querySelector('.area').value = '-' + result; } } const body = document.querySelector("body"), modeToggle = document.querySelector(".drak-light"); modeToggle.addEventListener("click" ,() =>{ modeToggle.classList.toggle("active"); body.classList.toggle("dark"); if(!body.classList.contains("dark")) { localStorage.setItem("mode", "light-mode"); } else { localStorage.setItem("mode", "dark-mode"); } }); |
Download source code:
If you are unable to use or connect the files with each other. Then we created a solution. The download button is given below.
If you have any questions related to the calculator in javascript then ask these questions in the comment section. We will try to clear your doubts related to the calculator.
Tasks and Conclusion:
It’s a great step to create a calculator in javascript. It will help you to understand a lot of libraries of javascript and properties of CSS. Now I am assigning you some tasks and they will lead you toward a great learning journey.
You have to change the screen size. Then change the position of the + button from the right below corner to the upper left corner. Also, change the color of these buttons. All the buttons have round corners. Just remove that from the buttons that made them round from corners. By doing all of these things you can create beautiful logic in your programming skill.
Thanks for reading this article.
Download Source CodeClick to Download