Operators-
- Introduction to Operator
- Definition of Operator
- Unary, Binary and Ternary Operator
- Types of Operator
- 1.Arithmetic Operator
- 2.Relational Operator
- 3.Logical Operator
- 4.Assignment Operator
- 5.Increment and Decrement Operator
Introduction
In any programming language programmer wants to perform operations in
written program.
An operators are special symbols used to perform operations using the
operands and operator.
e.g. c=a+b, Here a and b are operands and ‘+’ is operator.
Define Term Operator
Operator is a symbol that specifies what type of operation to be
performed.
Unary,Binary and Ternary Operator
Unary Operator :
In JavaScript, an operator acts on single variable called as unary
operator e.g. like a++ or a--.
Binary Operator :
An operator acts on two variables called as binary operator e.g.like
a+b.
Ternary Operator:
An operator acts on three variables called as ternary operator like
if(a>b)?exp1:expr2.
Types of Operators-
1.Arithmetic Operator
2.Relational Operator
3.Logical Operator
4.Assignment Operator
5.Increment and Decrement Operator
1.Arithmetic Operator-
In JavaScript, arithmetic operators are used in mathematical
expressions. Arithmetic operators are used to perform basic mathematical
operations like addition,substraction,multiplication,division and remainder
i.e.modulus.
For following table values of x=5 and y=4.
Operator Description Example
+(Addition) adding value of x and y x+y=9
-(Subtraction) subtracts value of y from x x-y=1
*(Multiplication) multiplies value x with y x*y=20
/(Division) divides operands x and y x/y=1.25
%(Modulus) returns remainder x%y=1
Important Note
In javascript '+' operator is also used for to combine two
strings.
e.g. var s1="India Is";
var s2="Great"
var s3=s1+s2;
Output-
India Is Great
2.Relational
Operator-
In JavaScript
relational operators are used for comparison of operands. Result of relational
operator is true or false i.e.boolean type. Relational operators are also
called as comparison operator.There are some operators in javascript given
below.
Operator Meaning Description Example Result
== equal to for checking equality 10==10 True
!= not equal to for checking inequality 40!=40 False
> greater than for check great value 40>40 False
< less than for check less value 15<5 True
>= greater than equal to check for greater and equal 7>=8 False
<= less than equal to check for less as well as equal 10<=10 True
3.Logical Operator-
When you want to combine
more than one condition that time logical operators are used.In JavaScript
there are three types as given below.
1.&& (Logical AND) -Evaluates true when both conditions are true
e.g.var a=9,b=8,c=6; if((a>b)&&(a>c)) both conditions are true so evaluate true.
2.|| (Logical OR) - Evaluates true when any one of true
e.g.var a=-1;if(n<0||n>100) first
condition true and second false so total expression value is true
3.! (Not) - used for negate or invert expression
e.g.var a=0;if(!a==0) expression
(a==0) result is true but it inverts into result false.
4.Assignment Operator-
In JavaScript when you want to assign
value of an expression to a variable assignment operator is used.
Example-a=0; result=x*y; a=b=c=9;
5.Increment and Decrement Operator-
To add or decrease the value of a
variable by 1, these operators are used. The operator ++ adds 1 to the operand
value and “ - - “ subtracts 1.Increment and decrement both operators are unary
operator.
1.++a pre increment value increased
before used in expression
2.x++ post increment value increased
after used in expression
3.- -x pre decrement value decreased
before used in expression
4.x- - post decrement value decrease
after used in expression
Example-
var a=10;
b=++a;
Output-b=11 and a=11
var a=10;
b=a++;
Output-b=10 and a=11
Question:
- What are types of operators in javascript?
- Do you know about javascript comparison operator?
- What do you mean by ternary operator in javascript?
- Javascript logical operators,assignment operator.
Variables and Data types Video
Share on Whatsapp |
Please Comment and Share. ConversionConversion EmoticonEmoticon