1. Java Script Expression with Binary Operator
In JavaScript, you can perform arithmetic by forming the expressions. When making the expression, you can use binary operators like addition (+), subtraction (-), multiplication (*) and division (/). We call them as a Binary Operator as it operates on two variants. Now look at the expression below:
1 |
10 – 4 * 2 |
You may expect the result is 12. But the result is 2. Because here Operator Precedence comes into picture. The operator * and / play higher priority than the + and – operators. In the above expression, the binary operation * is figured out on the operands 4, 2. Then the binary operation minus is done on the operand 10 and 8 giving up the result as 2. In the above expression, we used only the constant values. But one can use the variables also as an operand.
2. Unary Operator
The operator ++
and --
are called as unary operator. Because these operators operate on the single entity. The operator ++ will raise the operand value by 1 and — will reduce it by 1.
3. Performing Calculation
Look at the below statements. In line three, we have an expression. The expression involves two binary operators which perform multiplication operations. Since both operators are at equal precedence, the expression is carried out from left-to-right direction. So first, Pi * Radius
is done and then the result is multiplied with Radius again to finish the expression. The result is assigned to the variable Area by the binary assignment operator. The assignment (=) is also a binary operator, as it operates on two operands.
1 2 3 |
var Pi = 3,14 var Radius = 5 var Area = Pi * Radius * Radius |
4. JavaScript Expression Example
Now look at the example below:
Explanation
1) The function prompt will display a dialog to get user input. It is taking two parameters. The first param is a string which displays information text to the user. The second parameter appears in the text box. The displayed prompt is shown below. A user can accept the default value, or they can change it before clicking the OK button on the prompt dialog. Once the dialog closes, the variable Rad holds the user input.

2) Here, we form the expression based on the variable Rad and constant Pi (3.14). The variable Area holds evaluated expression result. As both operators are multiplication (*), JavaScript evaluates the expression from left to right.
3) We already saw about the alert function in the 001 example. Here, we use it again to display the calculated result in the Alert Box. The alert box is below:

5. Code Reference
002_PerformCalculation.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!DOCTYPE html> <html lang="en"> <head> <title>Javascript Calculation</title> </head> <body> <script> //Sample 01: Get Value for the Radius var Rad = prompt("Enter Radius of Circle", 10); //Sample 02: Form Expression & Perform Calculation var Area = 3.14 * Rad * Rad; //Sample 03: Show Result alert("Area of Circle is " + Area); </script> </body> </html> |
Categories: JavaScript
Tags: alert, binary operator, prompt, Unary Operator