Friday, 20 February 2015

Functions that return value are expressions. Many expressions use operators. The JavaScript language provides a rich set of operators to perform a wide variety of operations. Operators are divided into:
  • Mathematical Operators
  • Relational Operators
  • Logical Operators

Mathematical Operators
The mathematical operators perform the common operations of addition (+), subtraction (-), division (/), and multiplication (*). The + operator is also used to concatenate (join together) strings. JavaScript knows whether to add or concatenate based on the data.

Note-
That if an expression contains multiple operators, the / ill be performed first followed by the + and -. Thus 10 + 8/4 evaluates to 12 because the division is done before the addition. You can modify the order of operations by using parentheses. Anything within parentheses is done first. For example, (10 + 5)/3 evaluates to 5.

Two other mathematical operators are the increment and decrement operators. They are used when you need to increase or decrease the value of a numerical variable by one. The operators are ++ and – can be placed either before or after a variable name. For an example:

++sum
sum++
--sum
sum—

The placement of the operator determines when the increment or decrement is performed. Let us discuss this point more:
  • If the operator is before the name, the operation in performed before the value of the variable is used in the expression.
  • If the operator is after the name, the operation in performed after the value of the variable is used in the expression.

Look at these examples:

var count = 5;
var n1, n2;
n1 = count -- + 10;

The variable n1 will have the value 15 because the original value of count, 5, is used in the expression and then count is decremented to 4.

var count = 5;
var n1, n2;
n2 = --count + 10;

Here, the variable n2 will have the value 14 because the value of count is decremented to 4 before it is used in the expression.
You can combine operation and assigning of results in a single operation. For example:
sum = sum + 10;

This increases value of sum by 10. Here, you can use the assignment with addition operator (+=) to do this as follows:

sum += 10;

which will do the same operation as above.
Similarly for subtraction (-=), multiplication (*=), and division (/=) can be used.

Relational Operators
Programs often need to compare one value with another. These operators, which can be used for strings as well as numbers, return the logical values true or false depending on the result of the comparison.
All relational operators are given in next table:
OPERATOR
SYMBOL
DESCRIPTION
Equal to
==
Returns true if the values being compared are equal. Returns false otherwise.
Not equal to
!=
Returns if the value being compared are not equal. Returns false otherwise.
Greater than
>
Returns true if the first value is larger than the second value. Returns false otherwise.
Less than
<
Returns true if the value is less than the second value. Returns false otherwise.
Greater than or equal to
>=
Returns true if the first value is larger than or equal to the second value. Returns false otherwise.
Less than or equal to
<=
Returns true if the first value is less than or equal to the second value. Returns false otherwise.

The use of the relational operators with numerical values is normal, as expected. 2>5 gives False and 5=5 gives True. But, with strings it is a bit more complex. Characters are represented by numerical codes (ASCII) in the computer. When you compare strings, it is these codes that are actually compared, on a character-by-character basis. For example, would consider “ant” to be less than or greater than “elephant”? Most people will say less than because ‘a’ comes before ‘c’ in the alphabet. The numerical code (ASCII value) for a is 97, for b 98, and in the same upto 122 for z, so fortunately the comparisons work as you had expected.
But wait, what about comparing “ant” with “ELEPHANT”? You may be surprised to find that it will result in “ant” to be greater than “ELEPHANT”. This is because the uppercase letters have numeric code in the range 65-90, so any uppercase letter is “less than” any lowercase letter. These all examples might sound useless or funny but that is the way it works. So, you have to be aware of the difference as such between upper and lower case. You can use the toUpperCase() and toLowerCase() methods for this. These methods do not change the original value but just make temporary copy. Here’s an example:

var s1 = “ant”;
var s2 = “ELEPHANT”;
alert(s1 < s2); //It gives output as ‘False’
alert(s1.toUpperCase()< s2.toUpperCase());//It gives output as ‘True’

Logical Operators
The logical operators perform operations on the logical values true and false. The simplest logical operator is the NOT operator, represented by the symbol !. It reverse the value of its operand, from true to false or vice versa. For example:

var a = true;
var b = !a; // b will have the value false.

The other logical operators inputs two individual logical expressions to form a single logical value:
  • AND (&&) returns true if both of its operand are true and returns false otherwise (if one or both operands are false).
  • OR (| |) returns true if either one or both its operands are true and returns false otherwise (if both operands are false).

See the table below to understand them carefully:
Value 1
Value 2
AND (&&)
OR (| |)
True
True
True
True
True
False
False
True
False
True
False
True
False
False
False
False

Next example show use of Logical operators:
Here are some examples:

var a = true;
var b = false;
var c = true;
var x;
x = a | | b; // x is true
x = a && b; // x is false.
x=b | | !c; // x is false (note the NOT operator)


0 comments:

Post a Comment