OPERATORS
TYPES OF OPERATORS:
1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations on numeric values. Common examples include:
Addition (+): Adds two operands.
Subtraction (-): Subtracts the right operand from the left.
Multiplication (*): Multiplies two operands.
Division (/): Divides the left operand by the right.
Modulus (%): Returns the remainder of a division operation.
Increment (++) and Decrement (--): Increase or decrease a value by one.
Example:
1. 10 + "2"= 102(concatination)
2. "Name"+2= "Name2"
3. 10 - "2"= 8
4. 5 * "5"= 25
2. Relational Operators
Relational operators compare two values and return a Boolean result (true or false). Examples include:
Equal to (==)
Not equal to (!=)
Greater than (>)
Less than (<)
Greater than or equal to (>=)
Less than or equal to (<=)
These operators are commonly used in conditional statements and loops.
Example:
1. 10 == 10 //true
2. 10 ==="10" //false
3. 10! == 10 //false
3. Logical Operators
Logical operators are used to combine or invert Boolean expressions:
AND (&&): True, only if both operands are true.
OR (||): True if at least one operand is true.
NOT (!): Inverts the Boolean value of an operand
Example:
1. i=5; j=10; => i--<5 && ++j>10 => //false
2. i=5; j=10; => i--<10 && ++j>10 => //true
3. i=5; j=10; => i--<5 || ++j>10 => //True
4. Bitwise Operators
Bitwise operators perform operations at the binary level on integer values:
AND (&), OR (|), XOR (^)
Left shift (<<), Right shift (>>)
These are useful for low-level programming, such as manipulating flags or performing efficient calculations
5. Assignment Operators
Assignment operators assign values to variables and can also combine operations:
Simple assignment (=)
Add and assign (+=), Subtract and assign (-=), Multiply and assign (*=), etc.
They simplify code by combining arithmetic and assignment in one step.
6. Conditional (Ternary) Operator
The conditional operator (?:) evaluates a condition and returns one of two values based on whether the condition is true or false:
Example
let mark: 89;
let result;
if (mark >=35){
if (attendance>70){
result="pass";
}
else{
result="fail";}
Top comments (0)