Projects are the best way to learn JavaScript, and a calculator is one of the best projects to choose. A calculator covers all the complex interactions with UI and JavaScript while still being simple enough for people of any skill level.
In this video I will walk you through the steps it takes to build a completely modern calculator using JavaScript best practices. If you want to learn JavaScript or improve your JavaScript skill this is a must do project.
📚 Materials:
Code: https://github.com/WebDevSimplified/Vanilla-JavaScript-Calculator
🧠 Concepts Covered:
– How to use ES6 classes to organize code
– How to sync JavaScript code with a UI
– CSS Grid
– Flexbox
– The best way to cleanly handle user input
– How to debug complicated edge cases
🌎 Find Me Here:
Twitter: https://twitter.com/DevSimplified
GitHub: https://github.com/WebDevSimplified
CodePen: https://codepen.io/WebDevSimplified
#JavaScript #WebDevelopment #Programming
Original source
32 responses to “Build A Calculator With JavaScript Tutorial”
It's a bit difficult way to make calculator for me. So many CSS values and pieces of JS that I haven't learned yet. But anyway thanks.
I love javascript, cuz it NEVER WORKS and when it doesnt work IT GIVES NO HINTS why its not working
class calculator{
constructor(previousOperandTextElement, currentOperandTextElement){
this.previousOperandTextElement = previousOperandTextElement
this.currentOperandTextElement = currentOperandTextElement
this.clear()
}
clear(){
this.currentOperand= ''
this.previousoperand = ''
this.operation = undefined
}
delete() {
}
appendNumber(number){
if (number === '.' && this.currentOperand.includes('.')) return
this.currentOperand = this.currentOperand.toString() + number.toString()
}
chooseOperation(Operation) {
if (this.currentOperand === '') return
if(this.previousoperand !== ''){
this.compute()
}
this.operation = operation
this.previousoperand = this.currentOperand
this.currentOperand = ''
}
compute() {
}
updateDisplay(){
this.currentOperandTextElement.innerText=this.currentOperand
this.previousOperandTextElement.innerText=this.previousoperand
}
}
const numberButtons = document.querySelectorAll('[data-number]')
const operationButtons = document.querySelectorAll('[data-operation]')
const equalsButton = document.querySelector('[data-equals]')
const deleteButton = document.querySelector('[data-delete]')
const allClearButton = document.querySelector('[data-all-clear]')
const previousOperandTextElement = document.querySelector('[data-previous-operand]')
const currentOperandTextElement = document.querySelector('[data-current-operand]')
const calculator = new calculator(previousOperandTextElement, currentOperandTextElement)
numberButtons.forEach(button => {
button.addEventListener('click', () => {
calculator.appendNumber(button.innerText)
calculator.updateDisplay()
})
})
oprationButtons.forEach(button => {
button.addEventListener('click', () => {
calculator.chooseOperation(button.innerText)
calculator.updateDisplay()
})
})
really great video i've learned a lot of new things about JS. Thanksss❤
2nd try: how can i add a negative input function?
Master, Accept my humble Respect from Bangladesh 💘💥
What i have to do If i want to add sin cos and tan funcation?
please give the code i copied the exact thing but its not coming same i am using sublime text 3
How do I round out the edges of buttons in calculator?
Am having issues calling my "calculator-grid"
can you slow down? nobody's running on ya!
Great tutorial! Wondering if there's a reason why you didn't put the script tag at the bottom of the body instead of deferring it?
You're a genius!
Excellent video! More please 🙂
Thanks a lot for another great tutorial! One thing many have probably pointed already, in my opinion, you should really work on the pace of your speech, you speak extremely fast when explaining things, and slowing down the playback time it's quite annoying cause the voice is all distorted (imo). This might cause some viewers to quit your tutorial halfway through. Your content and logic are great and clear, and the use of data instead of classes in HTML for the reference in JS is super smart. Keep up the great work, you're killing it!
How to writing coad
Software name?
I had to wrap my code within a window.addEventListener('load', function) to get it work, any idea why?
How he can use in clear function (this.previousOperand, this.currentOperand) when he defined it like a this.previousOperandTextElement and this.currentOperandTextElement because it's two different functions? Don't get it
Nice video, but In the very begining you executed 32+69 giving you the result of 101, then you multiplyed it by 36 and somewhat got 3.636 instead of 3636. But after you clicked "delete" it deleted the last charcter -> the result became 363 like it was correct result but had visual comma bug. Why so?
Thanks for including the code below the video. By the way, on your code, the division on the js file compute switch statement needs to be the same as the html file.
in my html file, my <script> tag is red for some reason. I think it's formatted correctly, but for some reason it's red. I need a fix because it doesn't output correctly
Can anybody give me a light? I can't click on any of the numbers on the calculator 🙁 the code is correct but I can't click on the numbers.
Cool! I added four buttons for memory support: mc, m +, m-, mr.
I have expanded the delete method so that it also allows you to delete the operator and the previous number: https://codepen.io/abmstudio/pen/oNvwPWV
i have one problem so far… 🙂 I stopped on 22min. console log give me this error = Uncaught TypeError: Cannot read property 'toString' of undefined
at Calculator.appendNumber (main.js:13)
at HTMLButtonElement.<anonymous> (main.js:44)
Why it is showing – document is not defined in script.js file..where we are declaring variables
im getting errors all over the place in the CSS file ;-;
Hey great job on the tutorials! I learn more from your projects than from many of the online courses I take! Do you have a recommendation for which order to watch your JavaScript project tutorials in if you're a beginner? Like from easiest to hardest? Thank you!
Thank you so much. This video is SO HELPFUL.
AWESOME!!! Very practical tutorial and easy to understand. hope to see more tutorials like this.
Very thorough tutorial. Great work.
finalmente um branquinho gostoso não tava mais aguentando indiano nessa porra
I'm stuck at 5:54, nothing changes after I add gradient.. when I try to open With Live Server. I thought I made some mistakes but I copied your source code and tried to Live it, still all I get is the result at 5:54…