Using Javascript number operators.
Mathematical Number operators.
-
To create numeric expressions we are using mathematical operators which can be:
Operator Example Comments +
(addition)var sum1=50, sum2=30; var sum = sum1 + sum2; // sum will be 80
Used to add one operand with another operand. -
(subtraction)var sum1=50, sum2=30; var sum = sum1 - sum2; // sum will be 20
Used to subtract one operand with another operand. *
(multiplication)var price=10, quantity=5; var sum = price * quantity; // sum will be 50
Used to multiplicate one operand with another operand. /
(division)var price=10, sum=5; var quantity = sum / price; // quantity will be 0.5
Used to devide one operand with another operand. %
(modulo)var price=5, sum=11; var rest = sum % price; // rest will be 1
Used to get remainder from one operand with another operand. -
(unary)var sum2=11; var sum = -sum2; // sum will be -11
Converts a positive value to an equivalently negative value, and vice versa. +
(unary)var sum2=11; var sum = +sum2; // sum will be 11
The operator does nothing; it simply evaluates to the value of its argument. -
Expression can also be of incrementing and decrementing types:
The two first one has both two options which is prefix and postfix:
var count=10 count++; // Start with count and increment it. count--; // Start with count and decrement it. // OR count = count + 1; // Start with count and increment it with a value (1). counter = count - 1; // Start with count and decrement it with a value (1). // OR count += 1; // Start with count and increment it with a value (1). count -= 1; // Start with count and decrement it with a value (1).
var a = ++c; // prefix operator - increments c and assign it to a var b = c++; // postfix operator - assign c to b and then increment c var a = --c; // prefix operator - decrements c and assign it to a var b = c--; // postfix operator - assign c to b and then decrement c
<script type="text/javascript"> var value = 1; var result = value++ * 5; document.write("result: "+ result); document.write(" and value: "+ value); document.write("<br>"); // prints: result: 5 and value: 2 document.write("result: "+ --result); document.write(" and value: "+ value++); document.write("<br>"); // prints: result: 4 and value: 2 // but the result=4 and the value=3 result = (++value * 5) ; document.write("result: "+ result); document.write(" and value: "+ value); document.write("<br>"); // prints: result: 20 and value: 4 document.write("result: "+ ++result); document.write(" and value: "+ --value); document.write("<br>"); // prints: result: 21 and value: 3 </script>
© 2010 by Finnesand Data.
All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the
correctness in the pages or source codes.
The risk of using this web-site pages or any program
codes from this website is entirely at the individual user.