1. Java Script Expression with Binary Operator
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
-- 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.
var Pi = 3,14
var Radius = 5
var Area = Pi * Radius * Radius
Now look at the example below:
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.
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
//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);
Tags: alert, binary operator, prompt, Unary Operator
Do you like this Example? Please comment about it for others!!