Build a Calculator with VueJS Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report We'll learn how to build a simple calculator app using Vue.js. A calculator is a fundamental tool for performing mathematical calculations, and building one using Vue.js is a great way to understand the basics of Vue.js components, data binding, and event handling. Step-by-step guide to set up the projectStep 1: Install Vue CLI Globallynpm install -g @vue/cliStep 2: Create a New Vue.js Projectvue create vue-calculatorStep 3: Navigate to your project directorycd vue-calculatorProject Structure: Step 4: Install Required Dependenciesnpm install axiosStep 5: Changing the filesReplace the code of App.vue with the given code belowUpdate main.js create Calculator.css for stylingExample: This example shows the calculator using VueJs. CSS /*Calculator.css*/ #app { font-family: Arial, sans-serif; text-align: center; margin-top: 40px; } .title { color: green; } .calculator { width: 300px; margin: 0 auto; border: 1px solid #ccc; border-radius: 5px; padding: 20px; } .result { width: 90%; padding: 10px; font-size: 20px; text-align: right; margin-bottom: 10px; } .buttons { display: grid; grid-template-columns: repeat(4, 1fr); grid-gap: 10px; } button { padding: 15px; font-size: 18px; cursor: pointer; border: none; outline: none; } .number, .operator { background-color: #f0f0f0; } .clear, .equal { background-color: #ff6666; color: #fff; } button:hover { background-color: #ddd; } .equal { grid-column: span 2; } JavaScript //App.vue <template> <div id="app"> <h1 class="title">GeeksForGeeks</h1> <h2>Calculator App</h2> <div class="calculator"> <input type="text" class="result" :value="result" readonly /> <div class="buttons"> <button class="number" @click="handleClick('7')">7</button> <button class="number" @click="handleClick('8')">8</button> <button class="number" @click="handleClick('9')">9</button> <button class="operator" @click="handleOperatorClick('/')">/</button> <button class="number" @click="handleClick('4')">4</button> <button class="number" @click="handleClick('5')">5</button> <button class="number" @click="handleClick('6')">6</button> <button class="operator" @click="handleOperatorClick('*')">*</button> <button class="number" @click="handleClick('1')">1</button> <button class="number" @click="handleClick('2')">2</button> <button class="number" @click="handleClick('3')">3</button> <button class="operator" @click="handleOperatorClick('-')">-</button> <button class="number" @click="handleClick('0')">0</button> <button class="number" @click="handleClick('.')">.</button> <button class="number" @click="handleClick('00')">00</button> <button class="operator" @click="handleOperatorClick('+')">+</button> <button class="clear" @click="handleClear">C</button> <button class="clear" @click="handleClearEntry">CE</button> <button class="equal" @click="calculate()">=</button> </div> </div> </div> </template> <script> export default { name: 'App', data() { return { result: '', calculated: false // Flag to track if calculation has been done }; }, methods: { handleClick(value) { if (this.calculated) { // If calculation has been done, // start a new expression this.result = value; this.calculated = false; // Reset flag } else { this.result += value; } }, handleClear() { this.result = ''; this.calculated = false; // Reset flag }, handleClearEntry() { if (this.result && this.result.length > 0) { this.result = this.result.slice(0, -1); if (this.result.length === 0) { this.handleClear(); } } else { this.handleClear(); } }, handleOperatorClick(operator) { // If the last character is an operator, // replace it with the new operator if (/[+*/-]$/.test(this.result)) { this.result = this.result.slice(0, -1) + operator; } else { // Otherwise, add the new operator this.result += operator; } this.calculated = false; // Reset flag }, calculate() { try { let evaluatedResult = eval(this.result. replace(/(^|[^0-9])0+(\d+)/g, '$1$2')); if (evaluatedResult === Infinity || evaluatedResult === -Infinity) { throw new Error('Divide by zero error'); } this.result = Number.isFinite(evaluatedResult) ? evaluatedResult : 'Error'; this.calculated = true; // Set flag to true after calculation } catch (error) { if (error.message === 'Divide by zero error') { this.result = 'Error: Divide by zero'; } else { this.result = 'Error'; } } } } }; </script> <style src="./Calculator.css"> /* Add your styles here */ </style> JavaScript //main.js // Import createApp from Vue import { createApp } from 'vue'; import App from './App.vue'; // Create a Vue app instance const app = createApp(App); // Mount the app to the DOM app.mount('#app'); Steps to run the Application: npm run serveVisit http://localhost:8080 in your browser to see the Vue Calculator app running. Output: Comment More infoAdvertise with us Next Article ReactJS Calculator App (Building UI) A ayushbhankr3p Follow Improve Article Tags : Project JavaScript Web Technologies Vue.JS Similar Reads ReactJS Calculator App (Building UI) We created our first app and deleted all those files we did not need, and created some of the files that we will need in the future. Now as we stand in our current situation we have a blank canvas before us where we will have to create our calculator app. We will be creating the project in multiple 4 min read Build a Loan Calculator using Next.js The Loan Calculator allows users to determine loan amount, interest rate, and tenure, calculate monthly mortgage payments, total loan payments over tenure, and total interest payable. In this article, we will walk you through the process of building a Loan Calculator using Next.js. Let's take a look 4 min read Build a Calculator using React Native Building a simple calculator app in React Native is an exciting project. You'll create an interface with buttons for numbers (0-9) and operations (+, -, Ã, ÷), along with a display area for showing input and results. Using React Native, you'll write functions that handle button presses and perform c 10 min read Building a Shopping List with VueJS The following approach covers how to build a simple shopping list app with VueJs. Vue (or Vue.js) is an open-source JavaScript framework geared towards building user interfaces, it was created by Evan You.Prerequisites:Basic understanding of HTML.Basic knowledge of CSSBasic knowledge of JavaScriptIn 3 min read Build a Calculator with React, Tailwind, and Django This article will guide you in creating a calculator using React and Tailwind with the Django Framework. We'll explore the integration of Django, React, and Tailwind, and go through the step-by-step process of implementing the calculator.What is a Calculator?A calculator is a device or tool designed 6 min read Calculator App Using Angular This Calculator app will provide basic arithmetic functionalities such as addition, subtraction, multiplication, and division, with a clean and modern UI. It will be fully responsive and interactive, ensuring a great user experience across different devices. This project is suitable for both beginne 4 min read Like