Operators in Java Script
Operators in JavaScript Course Help:
Operators which perform operations on operands and yields output. Java script consists 8 categories of operators such as:
- Arithmetic operators
- Relational operators
- Logical operators
- Assignment operators
- Increment and Decrement operators
- Conditional operators
- Bitwise operators
- Special operators
Operators in JavaScript Assignment Help Through Online Tutoring and Guided Sessions at MyAssignmentHelp
6.1 Arithmetic operators
It is used for basic arithmetic operations. They are ‘+’, ‘-‘, ‘*‘, ‘/’ and ‘%‘. The following example illustrates the use of arithmetic operators.
Example:
6.2 Comparison Operators
The connection among operands in the expression is retrieved using comparison operators. Java Script supports 6 comparison operators.
Comparison Operators in Java Script
Comparison operators are most commonly used in control statements such as if, while, until etc.
Let us consider two variables x=10 and y=5, the simple comparisons are
Example
6.3 Logical Operators
Comparison operations are joined using Logical operators. Three logical operators are there in Java script. They are:
Let us consider two variables x=20 and y=10, the simple logical operations areExpression | Boolean Value | Boolean value | Output |
(x>=20)&&(y=<10) | (True) && (False) | False | 0 |
(x>=20)||(y>10) | (True) || (False) | True | 1 |
!((x>=20)&&(y<10)) | !((true) && (False)) | True | 1 |
Example:
6.4 Assignment Operator
Assignment operators are assigning values after performing operations on operand. ‘=’ is the assignment operator in java script.
Operators | Expression | Output |
= (simple) | x=2 | 2 is assigned to variable a |
+= | x+=2 | x=x+2 if x = 2 means output of a will be 4 |
-= | x-=2 | x=x-2 if x = 2 means output of a will be 0 |
*= | x*=2 | x=x*2 if x = 2 means output of a will be 4 |
/= | x/=2 | x=x/2 if x = 2 means output of a will be 1 |
%= | x%=2 | x=x%2 if x = 2 means output of a will be 0 |
&= | x&=25 |
x=x&25 if x=12 means Bit Operation of 12 and 25 x(12) = 00001100 & (25)= 00011001 ________ 00001000 = 8 (In decimal) Output will be 8 |
^= | x^=25 | x=x^25 |
Example:
6.5 Increment and Decrement Operators
Program:
6.6 Conditional operators
Conditional operator is used check conditions also termed as ternary operator. It has three parts such as test condition, statement1 if test condition is true and statement2 if test expression is false. It follows the same logic of If..Else.
Conditional operator is written as:
Test_condition ? statement1: statement2
Example:
6.7 Bitwise Operators
Java script has the ability to support mathematical operations like addition, subtraction, multiplication and division at binary level (0’s and 1’s). The binary level operations are accomplished with the help of 6 bitwise operators.
- & (bitwise AND) : It takes two integer numbers are operands.
- | (bitwise OR) : It performs bitwise OR on two integers. The result of OR is 1 any of the two bits is 1.
- ^ (bitwise XOR) : It Takes two numbers as integer operands. XOR produces the result ‘1’ or ‘true’ if one of the value is true. If both values are true the result will be false.
- << (left shift) : It takes two argument, the first argument is the operand in which left shifting is performed, the second argument decides number of moves on left side.
- >> (right shift) : It takes two argument, the first argument is the operand in which left shifting is performed, the second argument decides number of moves on left side.
- ~ (bitwise NOT) : It is complement operator. It converts ‘1’ to ‘0’ and vice-versa.
Example:
Let us consider that we have 2 operands op1 and op2 are represented by binary values as follows:
op1 = 0000 1101
op2 = 0001 1001
&---------------------------
0000 1001
-----------------
Example:
op1 = 0000 1101
op2 = 0001 1001
|---------------------------
0001 1101
-----------------
Example:
op1 = 0000 1101
op2 = 0001 1001
^---------------------------
0001 0100
-----------------
Example:
11010100<<1 = 110101000
11010100<<4 =110101000000
Example:
11010100>>2 = 00110101
11010100>>8 = 00000000
Example:
~ 00100011
------------------
11011100
-----------------
6.8 Special Operator in Java script
Type of Operator
The data type of an operand is returned by the operator.
Example
- Namespaces
- Preprocessor Directives
- Regular Expressions
- Exception Handling
- File I/O
- Attributes
- Reflection
- Properties
- Indexers
- Delegates
- Events
- Collections
- Generics
- Anonymous Methods
- Unsafe Code
- Multithreading
- Variables Declarations
- Build In Types
- Operators
- Unary Operators
- Binary Operators
- Array Operations