What is an operator?
JavaScript has both binary and unary operators, and one special ternary operator, the conditional operator. A binary operator requires two operands, one before the operator and one after the operator:
operand1 operator operand2
For example, 3+4 or x*y.
For example, 3+4 or x*y.
A unary operator requires a single operand, either before or after the operator:
operator operand
or
operand operator
For example, x++ or ++x.
Let us take a simple expression 5 + 5 is equal to 10. Here 5 and 5 are called operands and ‘+’ is called the operator.
or
operand operator
For example, x++ or ++x.
JavaScript supports the following types of operators.
- Arithmetic Operators
- Comparision Operators
- Logical (or Relational) Operators
- Assignment Operators
- String Operators
- Conditional (or ternary) Operators
Using this operator we easly add,subtract,multiply,divide etc like see in example. Operator Description Example Result.
Arithmetic Operators
Operator | Description | Example | Expression | Result |
+ | Addition | x=2 y=2 |
x+y | 4 |
- | Subtraction | x=5, y=3 | x-y | 2 |
* | Multiplication | x=5,y=5 | x*y | 25 |
/ | Division | x=15,y=5 | x/y | 3 |
% | Modulus (division remainder) | x=5,y=2 | x%y | 1 |
++ | Increment | x=5 | x++ | 6 |
-- | Decrement | x=5 | x-- | 4 |
Comparison Operators
Operator | Description | Example |
== | is equal to | 5==8 returns false |
=== | is equal to (checks for both value and type) | x=5,y="5" x==y returns true x===y returns false |
!= | is not equal | 5!=8 returns true |
> | is greater than | 5>8 returns false |
>= | is Greter than equal | 5 >=8 returns false |
< | is Less than | 5 <8 returns true |
<= | is Less than equal | 5 <=8 returns true |
Logical Operators
Operator | Description | Example |
&& | and | x=6 y=3 (x < 10 && y > 1) returns true |
|| | or | x=6 y=3 x==5 || y==5) returns false |
! | Not | x=5 y=2 !(x==y) returns true |
Assignment Operators
In this opertor You can assign value in variable in javascript
Syntex - var x= value;
Example - var x = 5;
= this is Assign Operator you can assign any value using this operator. In this example assign 5 value in x variable and use this
String Operators
A string is most often text, for example "Hello World!". To stick two or more string variables together, use the + operator.
txt1="How a very";
txt2="nice flower
txt3=txt1+txt2;
txt2="nice flower
txt3=txt1+txt2;
The variable txt3 now contains "How a verynice flower!". To add a space between two string variables, insert a space into the expression, OR in one of the strings.
txt1="How a very";
txt2="nice flower!";
txt3=txt1+" "+txt2;
or
txt1="How a very ";
txt2="nice flower!";
txt3=txt1+txt2;
txt2="nice flower!";
txt3=txt1+" "+txt2;
or
txt1="How a very ";
txt2="nice flower!";
txt3=txt1+txt2;
The variable txt3 now contains "How a very nice flower!".
Conditional Operators
JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
Syntax
variablename=(condition)?value1:value2
Example
greeting=(visitor=="PRES")?"Dear President ":"Dear ";
If the variable visitor is equal to PRES, then put the string "Dear President " in the variablevariablename=(condition)?value1:value2
Example
greeting=(visitor=="PRES")?"Dear President ":"Dear ";
named greeting. If the variable visitor is not equal to PRES, then put the string "Dear " into the variable named greeting.
0 comments :
Post a Comment