What are javascript operators?
JavaScript Operators:
Javascript operators be a symbols related from the math, operators worked with one or multiple operands (Operands means the constants or variables).
Operands Operator operands Operator operands Then Results
As: 3 - 2 + 10 = 11
Javascript Operators are following :- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical (Relational) Operators
- Conditional (ternary) Operators
1. JavaScript Arithmetic Operators
| Operator | Description |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (Remainder) |
| ++ | Increment |
| -- | Decrement |
2. JavaScript Assignment Operators
| Operator | Example | Same As |
|---|---|---|
| = | x = y | x = y |
| += | x += y | x = x + y |
| -= | x -= y | x = x - y |
| *= | x *= y | x = x * y |
| /= | x /= y | x = x / y |
| %= | x %= y | x = x % y |
3. JavaScript Comparison Operators
| Operator | Description |
|---|---|
| == | equal to |
| === | equal value and equal type |
| != | not equal |
| !== | not equal value and not equal type |
| > | greater than |
| < | less than |
| >= | greater than or equal to |
| <= | less than or equal to |
| ? | ternary operator |
4. JavaScript Logical (Relational) Operators
| Operator | Description |
|---|---|
| && | logical and |
| || | logical or |
| ! | logical not |
Usage of the && Operator
| Example | Operand 1 | Operand 2 | Result |
|---|---|---|---|
| (2<3)&&(4>2) | true | true | true |
| (2<3)&&(4<2) | true | false | false |
| (3<2)&&(4>2) | false | true | false |
| (3<2)&&(4<2) | false | false | false |
If have anyone operands false then result surly would be "false"
Usage of the || Operator
| Example | Operand 1 | Operand 2 | Result |
|---|---|---|---|
| (2<3) || (4>2) | true | true | true |
| (2<3) || (4<2) | true | false | true |
| (3<2) || (4>2) | false | true | true |
| (3<2) || (4<2) | false | false | false |
If have anyone operands true then result surly would be "true"
5. JavaScript Conditional (ternary) Operators
Ternary Operator: we indicate from the ? symbol. This is same as if else condition but we can use this in a single line as syntax is below:
condition ? expression1 : expression2
Expression1 means "true" value and expression2 means "false" value always.
Ternary Operator Example:
var age = 15;
var ticket = (age > 12) ? 100 : 50;
console.log(ticket); // 100

No comments:
Note: Only a member of this blog may post a comment.