Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JavaScript 002 – Calculation via Expression

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:

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.

4. JavaScript Expression Example

Now look at the example below:

JavaScript Expression Evaluation
JavaScript Expression Evaluation

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.

JavaScript Promting for Value
JavaScript Promting for Value

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:

Showing the Result
Showing the Result

5. Code Reference

002_PerformCalculation.html

Categories: JavaScript

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.