Typescript comparison operators

Created By: chatGPT

TypeScript provides several comparison operators that you can use to compare values. These operators are similar to those found in JavaScript. Understanding how they work is important for making effective decisions in your code. Here are the primary comparison operators available in TypeScript:Equality Operators: These check whether two values are equal.
Strict Equality (===): Checks if two values are equal and of the same type.
let a = 5;
let b = '5';
console.log(a === b); // false
Loose Equality (==): Checks if two values are equal, performing type conversion if necessary.
console.log(a == b); // true
Inequality Operators: These check whether two values are not equal.
Strict Inequality (!==): Checks if two values are not equal and not of the same type.
console.log(a !== b); // true
Loose Inequality (!=): Checks if two values are not equal, performing type conversion if necessary.
console.log(a != b); // false
Relational Operators: Used to compare values based on their order.
Greater than (>): Checks if the left value is greater than the right value.
console.log(10 > 5); // true
Less than (<): Checks if the left value is less than the right value.
console.log(10 < 5); // false
Greater than or Equal (>=): Checks if the left value is greater than or equal to the right value.
console.log(10 >= 10); // true
Less than or Equal (<=): Checks if the left value is less than or equal to the right value.
console.log(10 <= 5); // false
Using Comparison Operators with Types: TypeScript provides type safety. Here's an example of how comparison operators can be used with different types.
let num: number = 10;
let str: string = '10';
if (num == str) {
    console.log('Loose equality: num is loosely equal to str');
}
if (num === str) {
    console.log('Strict equality: num is strictly equal to str');
} else {
    console.log('Strict equality: num is not strictly equal to str');
}
Conclusion: Using comparison operators effectively allows you to create complex logic in your programs. Always consider whether you need a strict or loose comparison, especially when dealing with different data types.
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