Typescript operators

Created By: chatGPT

In TypeScript, operators are symbols that perform operations on operands. Understanding the different types of operators is crucial for writing effective TypeScript code. Here are some common categories of operators you should know:
Arithmetic Operators are used to perform basic mathematical operations. The common arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).
let a: number = 10;
let b: number = 3;
let sum: number = a + b;  // 13
let difference: number = a - b; // 7
let product: number = a * b; // 30
let quotient: number = a / b; // 3.33
let modulus: number = a % b; // 1
Comparison Operators are used to compare two values. They return a boolean value (true or false). The main comparison operators include equal (), not equal (!=), strict equal (=), strict not equal (!==), greater than (>), and less than (<).
let x: number = 5;
let y: number = 10;
let isEqual: boolean = (x == y); // false
let isStrictEqual: boolean = (x === 5); // true
let isGreater: boolean = (x > y); // false
let isLess: boolean = (x < y); // true
Logical Operators are used to combine multiple boolean expressions. The primary logical operators are AND (&&), OR (||), and NOT (!).
let isTrue: boolean = true;
let isFalse: boolean = false;
let andResult: boolean = isTrue && isFalse; // false
let orResult: boolean = isTrue || isFalse; // true
let notResult: boolean = !isTrue; // false
Bitwise Operators operate on binary representations of numbers. Common bitwise operators include AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>).
let m: number = 5; // 0101 in binary
let n: number = 3; // 0011 in binary
let andBitwise: number = m & n; // 1 (0001)
let orBitwise: number = m | n; // 7 (0111)
let xorBitwise: number = m ^ n; // 6 (0110)
let notBitwise: number = ~m; // -6 (inverts bits)
Assignment Operators are used to assign values to variables. The most common assignment operator is the equals sign (=). There are also compound assignment operators like +=, -=, *=, etc., which combine assignment with another operation.
let value: number = 10;
value += 5;  // value is now 15
value -= 3;  // value is now 12
value *= 2;  // value is now 24
value /= 4;  // value is now 6
Ternary Operator is a shorthand for the if-else statement. It takes three operands: a condition, a result if the condition is true, and a result if the condition is false.
let isAdult: boolean = true;
let ageCheck: string = isAdult ? 'Adult' : 'Minor'; // ageCheck is 'Adult'
Understanding and effectively using TypeScript operators will significantly enhance your coding skills and help you write cleaner, more efficient code. Practice using these operators in different expressions to gain a better grasp of their functionality.
Introduction And SetupVariablesData TypesAnyUnknownVoidNeverStringNumberBooleanArrayTupleEnumObjectInterfaceType AliasMethodsFunctionArrow FunctionReturn TypeParametersDefault ParametersOptional ParametersRest ParametersControl FlowIf StatementElse StatementSwitch StatementFor LoopWhile LoopDo While LoopFor...of LoopFor...in LoopBreak StatementContinue StatementFunctionsFunction OverloadingRecursive FunctionExpression FunctionOperatorsArithmetic OperatorsAssignment OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsConditional (ternary) OperatorData StructuresArrayTupleObjectMapSetCommentsSingle Line CommentMulti Line CommentSyntaxType AnnotationsType InferenceNamespaceModuleExportImportDecoratorsAmbient DeclarationsEvent HandlingEvent ListenersAddEventListenerRemoveEventListenerError HandlingTry...catch StatementThrow StatementFinally BlockCustom Error TypesAsync AwaitPromisesGenericsConditional TypesMapped TypesIntersection TypesUnion TypesLiteral TypesDiscriminated UnionsType GuardsTypeof GuardInstanceof GuardAssertion FunctionsModule Augmentation