DEV Community

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

Posted on

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...

Latest comments (0)