3. Operator in Javascript
Operators in JavaScript
- Operators in JavaScript are very similar to the operators that appear in the other programming languages, such as C++, or Java.
- Operators are used extensively in JavaScript program to perform some sort of calculation, comparison, or assignment on one or more values.
Operator Sub-types in JavaScript
There are three sub-types of operators in JavaScript. They are:
1. Unary : Unary operator is an operator that takes only a single operand.
2. Binary : Binary operator is an operator that takes two operands.
3. Ternary : Ternary operator is an operator that takes three operands.
Precedence of Arithmetic Operators in JavaScript
There are two priority levels of arithmetic operation in JavaScript. They are as follows:
High priority: * / %
Low priority: + –
If two or more sets of parentheses occur into the expression one after another, the order of evaluation will be done from the left set towards the right set.
Types of Operators in JavaScript
In JavaScript programming language, operators are classified into eight different categories:
Arithmetic Operators
Assignment Operators
Comparison (Relational) Operators
Logical Operators
Bitwise Operators
Conditional Operators (Ternary )
1. Arithmetic (Mathematical) Operators in JavaScript
Arithmetic operators in JavaScript are used to perform the most common mathematical calculations or arithmetic operations on the operands, or two or more values.
- Addition (+)
- Subtraction (-)
- Multiplication(*)
- Division (-)
- Modulus (Remainder)(%)
2. Assignment Operator
- An operator that is used to assign/update the value of a variable is called assignment operator in JavaScript.
- The most common assignment operator is equal operator “=”, which assigns the right-hand value to the left-hand variable.
Types of Assignment Operator in JavaScript
1. Simple assignment
- Ex : var x = 20;
2. Compound assignment (ShortHand)
- Ex : x += 10;
3. Assignment as expression
-Ex : int a = b – c + 4;
3. Comparison Operators
- Comparison operators in JavaScript are those operators that are used for comparing between two numeric values, strings, objects, or two quantities.
Types of Comparison Operators in JavaScript
JavaScript supports eight types of relational or comparison operator
1. == (Equal) : This operator evaluates that the value of left and right operands are equal or not. If values on both sides of the operator are equal to each other, the equal operator (==) returns true.
2. != (Not Equal)
3. > (Greater than)
4. < (Less than)
5. >= (Greater than or Equal to)
6. <= (Less than or Equal to)
7. === (Strictly Equal or Identical) : This operator strictly checks whether the left and right values are equal and of the same data type. If the values on both sides are equal and of the same data type, the strict is equal to (===) operator returns true.
8. !== (Strictly not Equal or not Identical)
4. Logical Operators
- Logical operators in JavaScript are those operators which are used to form compound conditions by combining two or more simple conditions or relations.
- Types of Boolean Operators in JavaScript
1. && Logical AND
2. || Logical OR
3. ! Logical NOT
5. Conditional Operator
- Conditional operator in JavaScript provides a one line approach for creating a simple conditional statement.
- The conditional operator (?:) is also known as ternary operator because it takes three operands and perform a conditional test.
Ex : conditional_expression ? Value1 : Value2
Or,
variable = conditional_expression ? Value1 : Value2
var a = (x > y) ? 4 : 5
if condition true output should 4 else 5 .
Comments
Post a Comment