Operators in JavaScript. Operators are the symbols used to perform an operation
1. Arithmetic operators: It is a binary operator. It is used to perform add i-tion (+), subtraction(-), division(/), multiplication(*), modulus(%-gives the remainder), increment(++) and decrement(—) operations.
Eg. If x = 10 and y = 3 then
If x = 10 then
document.write(++x); → It prints 10+1=11
If x = 10 then
document.write(x++); → It prints 10 itself.
If x = 10 then
document.write(—x); It prints 10-1=9
If x = 10 then
document.write(x—);→ It prints 10 itself.
2. Assignment operators:
If a = 10 and b = 3 then a = b. This statement sets the value of a and b are same,i.e. it sets a to 3. It is also called short hands
If X = 10 and Y = 3 then
3. Relational(Comparison) operators:
It is used to perform comparison or relational operation between two values and returns either true or false.
Eg: lf X = 10 and Y= 3 then
4. Logical operators:
Here AND(&&), OR(||) are binary operators and NOT(!) is a unary operator. It is used to combine relational operations and it gives either true or false
If X = true and Y= false then
Both operands must be true to get a true value in the case of AND(&&) operation If X = true and Y = false then
Either one of the operands must be true to get a true value in the case of OR(||) operation If X = true and Y = false then
5. String addition operator(+):
This is also called concatenation operator. It joins (concatenates) two strings and forms a string.
Eg:
var x, y, z;
x= “BVM HSS”;
y= “Kalparamba”;
z = x + y;
Here the variable z becomes “BVM HSS Kalparamba”.
Note: If both the operands are numbers then addition operator(+) produces number as a result otherwise it produces string as a result. Consider the following.
Eg:
- 8(number) + 3(number) = 11 (Result is a number)
- 8 (number)+ “3”( string) = “83″ (Result is a string)
- “8” (string) + 3 (number) = “83”(Result is a string)
- “8” (string) + “3” (string) = “83” (Result is a string).