DEV Community

Cover image for Calculator Function using JavaScript 🚀🚀
Shubham Jadhav
Shubham Jadhav

Posted on

6 2

Calculator Function using JavaScript 🚀🚀

How to create calculator function in JavaScript??

Yes, Today we are making a JavaScript function which is return some mathematics output.

So, let's start with JavaScript code.

(1) Let's make a JavaScript
file and give any name
here I am giving index.js.
(Note:- We must need a
html file to run our JS
code in browser)

(2) Next step make a function
name calculator() in js
file. And pass three
parameter x, y and o.
Alt Text

(3) Parameters explanation :-
(a) x is a first number
(b) y is a second number
(c) o is a operator

(4) We are using switch
statement as shown in
following code.

img2

(4) Let's understand above
code :-
(a) first pass o
parameter because it
will decide which
mathematics operation
will conduct. o is
String and x and y
number
(b) If any user call this
function by passing three
parameters like
calculator(2,3,'+') then
the first case is
executed.
(c) When anyone not enter
valid input the it will
return "enter valid
numbers".

(5) Let's call the above
function and see output.
img3

👉 Happy Coding...
🙏 Thank you for reading...

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay