Javascript strict equality

Created By: chatGPT

Strict equality in JavaScript is checked using the === operator. This operator compares both the value and the type of two variables. Unlike the loose equality operator ==, which can perform type conversion, === prevents any such conversion, making it safer to use when you need an exact match.
const a = '5';
const b = 5;

console.log(a === b); // false
console.log(a === '5'); // true
console.log(b === 5); // true
In the code above, '5' is a string and 5 is a number. Using === returns false because the types differ. Conversely, using === with two identical strings returns true, as do identical numbers.
const obj1 = { key: 'value' };
const obj2 = { key: 'value' };

console.log(obj1 === obj2); // false
Even though obj1 and obj2 appear to be equal (they have the same structure and values), they are actually two distinct objects in memory. Therefore, obj1 === obj2 evaluates to false. To compare objects based on content, you would need to implement a custom comparison function.
function deepEqual(objA, objB) {
    return JSON.stringify(objA) === JSON.stringify(objB);
}

console.log(deepEqual(obj1, obj2)); // true
The deepEqual function above converts both objects to JSON strings and compares the strings. This approach works for simple objects, but be cautious: it may not handle complex structures like functions or circular references correctly.
console.log([1, 2, 3] === [1, 2, 3]); // false
Similarly, if you compare arrays with ===, it will return false because they are distinct objects in memory. If you want to compare the contents of arrays, you can also use a loop or a library like Lodash to assist in the comparison.
const array1 = [1, 2, 3];
const array2 = [1, 2, 3];

function arraysEqual(arr1, arr2) {
    return arr1.length === arr2.length && arr1.every((value, index) => value === arr2[index]);
}

console.log(arraysEqual(array1, array2)); // true
Introduction And SetupVariablesData TypesMethodsControl FlowFunctionsOperatorsData StructuresCommentsSyntaxEvent HandlingError HandlingPrimitive Data TypesReference Data TypesConstLetVarStringNumberBooleanNullUndefinedSymbolObjectArrayTemplate LiteralsArrow FunctionsRegular FunctionsCallApplyBindAsync FunctionsPromisesCallback FunctionsIf StatementElse StatementSwitch StatementFor LoopWhile LoopDo While LoopFor EachBreak StatementContinue StatementTry CatchThrowFinallyScopeClosureThis KeywordPrototypesInheritanceConstructor FunctionsClass SyntaxStatic MethodsGettersSettersSpread OperatorRest OperatorDestructuring AssignmentMap MethodFilter MethodReduce MethodEvent ListenersDom ManipulationInnerHTMLQuerySelectorAddEventListenerPreventDefaultStopPropagationTimeoutsIntervalsJsonLocal StorageSession StorageRegexMath ObjectDate ObjectString MethodsArray MethodsObject MethodsDeep CloningShallow CloningCallback HellPromise ChainingAsync AwaitMap Vs ForEachFilter Vs ReduceNullish Coalescing OperatorOptional ChainingTypeof OperatorInstanceof OperatorStrict EqualityLoose Equality