PROWAREtech








JavaScript: Tutorial - A Guide to JavaScript - Page 06
Subtract Operator
The subtract operator is a minus sign (-).
var result = 5 - 3;
alert(result);
	The subtract operator has special rules in dealing with the variety of type conversions present in JavaScript.
	If the two operands are numbers then perform arithmetic subtract and return the result. If either operand is NaN
	then the result is NaN. If Infinity is subtracted from Infinity then the result is
	NaN. If –Infinity is subtracted from –Infinity then the result is NaN.
	If –Infinity is subtracted from Infinitythen the result is Infinity. If
	Infinity is subtracted from –Infinity then the result is –Infinity. If +0 is
	subtracted from +0 then the result is +0. If –0 is subtracted from +0 then the result is –0. If –0 is subtracted from –0
	then the result is +0. If either operand is a string, a Boolean, null, or undefined then it is
	converted to a number and the arithmetic is calculated using these rules. If the conversion results in NaN, the result of
	the subtraction is NaN. If either operand is an object then its valueOf() method is called to retrieve a
	numeric value to represent it. If that value is NaN, the result of the subtraction is NaN. If the
	object does not have valueOf() defined, toString() is called and the resulting string is
	converted into a number.
var v1 = 4 - true;	// 3 because true is converted to 1
var v2 = 5 - false; // 5 because false is converted to 0
var v3 = NaN - 1;	 // NaN
var v4 = 3 - 5;	 // -2
var v5 = 6 - "";	// 6 because "" is converted to 0
var v6 = 6 - "1";	 // 5 because "1" is converted to 1
var v7 = 6 - null;	// 6 because null is converted to 0Multiplicative Operators
	There are three multiplicative operators: divide, modulus and multiply. If either
	operand in a multiplication operation is not a number then it is converted to a number behind
	the scenes using the Number() casting function. This means that an empty string is treated as 0,
	and the Boolean value of true is treated as 1.
Divide
The divide operator is a slash (/) and divides the first operand by the second.
var result = 55 / 10;
alert(result);
	The divide operator has special behaviors for special values. If the operands are numbers then regular arithmetic division
	is performed. Two positives or two negatives equal a positive. Operands with different signs results in a negative. If the
	result can not be represented then it returns either Infinity or –Infinity. If either operand is
	NaN then the result is NaN. If Infinity is divided by Infinity, the
	result is NaN. If zero is divided by zero then the result is NaN. If a non-zero finite number is
	divided by zero then the result is either Infinity or –Infinity. If Infinity is divided by any number then the result is either
	Infinity or –Infinity. If either operand is not a number then it is converted to a number behind the scenes using
	Number() and then these other rules are applied.
Modulus
The modulus operator (remainder operator) is a percent sign (%).
var result = 16 % 5;
alert(result);
	The modulus operator behaves differently for special values. If the operands are numbers then regular arithmetic division is
	performed. The remainder of the division is returned. If the dividend is an infinite number and the divisor is a finite
	number then the result is NaN. If the dividend is a finite number and the divisor is 0 then the result is NaN.
	If Infinity is divided by Infinity, the result is NaN. If the dividend is a finite number and the
	divisor is an infinite number, the result is the dividend. If the dividend is zero and the divisor is non-zero then the result is zero.
	If either operand is not a number then it is converted to a number behind the scenes using Number() and then the
	these rules are applied.
Multiply
The multiply operator is an asterisk (*) and multiplies two numbers.
var result = 3 * 5;
alert(result);
	The multiply operator behaves differently for special values. If the operands are numbers then regular arithmetic
	multiplication is performed. Two positives or two negatives equal a positive. Operands with different signs result in a
	negative. If the result cannot be represented, either Infinity or –Infinity is returned. If
	either operand is NaN then the result is NaN. If Infinity is multiplied by 0 then
	the result is NaN. If Infinity is multiplied by a finite number other than 0, the result is either
	Infinity or –Infinity. If Infinity is multiplied by Infinity then the
	result is Infinity. If either operand is not a number then it is converted to a number behind the scenes using
	Number() and then these rules are applied.