Which Statement Compares Two Elements

Loose equality is symmetric: A == B always has identical semantics to B == A for any values of A and B (except for the order of applied conversions). The behavior for performing loose equality using == is as follows:

  1. If the operands have the same type, they are compared as follows:
    • Object: return true only if both operands reference the same object.
    • String: return true only if both operands have the same characters in the same order.
    • Number: return true only if both operands have the same value. +0 and -0 are treated as the same value. If either operand is NaN, return false; so NaN is never equal to NaN.
    • Boolean: return true only if operands are both true or both false.
    • BigInt: return true only if both operands have the same value.
    • Symbol: return true only if both operands reference the same symbol.
  2. If one of the operands is null or undefined, the other must also be null or undefined to return true. Otherwise return false.
  3. If one of the operands is an object and the other is a primitive, convert the object to a primitive.
  4. At this step, both operands are converted to primitives (one of String, Number, Boolean, Symbol, and BigInt). The rest of the conversion is done case-by-case.
    • If they are of the same type, compare them using step 1.
    • If one of the operands is a Symbol but the other is not, return false.
    • If one of the operands is a Boolean but the other is not, convert the boolean to a number: true is converted to 1, and false is converted to 0. Then compare the two operands loosely again.
    • Number to String: convert the string to a number. Conversion failure results in NaN, which will guarantee the equality to be false.
    • Number to BigInt: compare by their numeric value. If the number is ±Infinity or NaN, return false.
    • String to BigInt: convert the string to a BigInt using the same algorithm as the BigInt() constructor. If conversion fails, return false.
Refer to more articles:  Which African Nation Has The Most Pyramids

Traditionally, and according to ECMAScript, all primitives and objects are loosely unequal to undefined and null. But most browsers permit a very narrow class of objects (specifically, the document.all object for any page), in some contexts, to act as if they emulate the value undefined. Loose equality is one such context: null == A and undefined == A evaluate to true if, and only if, A is an object that emulates undefined. In all other cases an object is never loosely equal to undefined or null.

In most cases, using loose equality is discouraged. The result of a comparison using strict equality is easier to predict, and may evaluate more quickly due to the lack of type coercion.

The following example demonstrates loose equality comparisons involving the number primitive 0, the bigint primitive 0n, the string primitive ‘0’, and an object whose toString() value is ‘0’.

Loose equality is only used by the == operator.

Related Posts

Which Type Of Therapy Is Best For Me Quiz

Which Type Of Therapy Is Best For Me Quiz

About Rational Emotive Behavior Therapy (REBT) and Cognitive Behavior Therapy Rational Emotive Behavior Therapy (REBT) and Cognitive Behavior Therapy (CBT) both follow the ABC model. “A” stands…

Which Nfl Kicker Won Mvp

Which Nfl Kicker Won Mvp

You may be interested Which Two Groups View Violations Of Gender Roles Most Negatively Which Of The Following Is True Regarding A Matrix-management Organization Which Of The…

Which Witch Is Which Lyrics

[Sparkle sound] Two little witches (two little witches) Played a little game (played a little game) They were very good friends (very good friends) Who looked the…

Which Of The Following Is A Cause Of Gynecological Emergencies

Surgical Management of Ectopic Pregnancy In the shocked patient with intraperitoneal bleeding, which is the typical presentation in the tropics, laparotomy should be undertaken. In subacute presentation…

Which Is The Beautiful Airport In The World

Which Is The Beautiful Airport In The World

Airports are more than just gateways to our destinations; they can also be architectural marvels and serene retreats. Some airports around the globe are designed to impress,…

Which Of The Following Is True Regarding Project Methodologies

Which Of The Following Is True Regarding Project Methodologies

Learning Objectives After completing this module, you will be able to:You may be interested Which Is Better Nra Or Uscca Which Year Lexus Rx 350 Is The…