Javascript is lower than

Less than (<)

The operands are compared with multiple rounds of coercion, which can be summarized as follows:

  • First, objects are converted to primitives by calling its [@@toPrimitive]() (with «number» as hint), valueOf() , and toString() methods, in that order. The left operand is always coerced before the right one. Note that although [@@toPrimitive]() is called with the «number» hint (meaning there’s a slight preference for the object to become a number), the return value is not converted to a number, since strings are still specially handled.
  • If both values are strings, they are compared as strings, based on the values of the Unicode code points they contain.
  • Otherwise JavaScript attempts to convert non-numeric types to numeric values:
    • Boolean values true and false are converted to 1 and 0 respectively.
    • null is converted to 0.
    • undefined is converted to NaN .
    • Strings are converted based on the values they contain, and are converted as NaN if they do not contain numeric values.

    Other operators, including > , >= , and

    • If one of the operands gets converted to a BigInt, while the other gets converted to a string that cannot be converted to a BigInt value (it throws a syntax error when passed to BigInt() ).
    • If one of the operands gets converted to NaN . (For example, strings that cannot be converted to numbers, or undefined .)

    For all other cases, the four operators have the following relationships:

     y === !(x >= y); x  y === !(x > y); x > y === y  x; x >= y === y  x; 

    Note: One observable difference between < and >is the order of coercion, especially if the coercion to primitive has side effects. All comparison operators coerce the left operand before the right operand.

    Examples

    String to string comparison

    "a"  "b"; // true "a"  "a"; // false "a"  "3"; // false 

    String to number comparison

    "5"  3; // false "3"  3; // false "3"  5; // true "hello"  5; // false 5  "hello"; // false "5"  3n; // false "3"  5n; // true 

    Number to Number comparison

    5  3; // false 3  3; // false 3  5; // true 

    Number to BigInt comparison

    Comparing Boolean, null, undefined, NaN

    true  false; // false false  true; // true 0  true; // true true  1; // false null  0; // false null  1; // true undefined  3; // false 3  undefined; // false 3  NaN; // false NaN  3; // false 

    Comparison with side effects

    Comparisons always coerce their operands to primitives. This means the same object may end up having different values within one comparison expression. For example, you may have two values that are both greater than and less than the other.

    class Mystery  static #coercionCount = -1; valueOf()  Mystery.#coercionCount++; // The left operand is coerced first, so this will return 0 // Then it returns 1 for the right operand return Mystery.#coercionCount % 2; > > const l = new Mystery(); const r = new Mystery(); console.log(l  r && r  l); // true 

    Warning: This can be a source of confusion. If your objects provide custom primitive conversion logic, make sure it is idempotent: multiple coercions should return the same value.

    Specifications

    Browser compatibility

    BCD tables only load in the browser

    See also

    Found a content problem with this page?

    This page was last modified on Feb 21, 2023 by MDN contributors.

    Your blueprint for a better internet.

    MDN

    Support

    Our communities

    Developers

    Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
    Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

    Источник

    Less than or equal (<=)

    The operands are compared using the same algorithm as the Less than operator, with the operands swapped and the result negated. x y are both false :

    • If one of the operands gets converted to a BigInt, while the other gets converted to a string that cannot be converted to a BigInt value (it throws a syntax error when passed to BigInt() ).
    • If one of the operands gets converted to NaN . (For example, strings that cannot be converted to numbers, or undefined .)
      When one of x or y is null , and the other is something that’s not null and becomes 0 when coerced to numeric (including 0 , 0n , false , «» , «0» , new Date(0) , etc.): x
      When one of x or y is undefined , and the other is one of null or undefined : x
      When x and y are the same object that becomes NaN after the first step of Less than (such as new Date(NaN) ): x
      When x and y are different objects that become the same value after the first step of Less than: x

    Examples

    String to string comparison

    "a"  "b"; // true "a"  "a"; // true "a"  "3"; // false 

    String to number comparison

    "5"  3; // false "3"  3; // true "3"  5; // true "hello"  5; // false 5  "hello"; // false 

    Number to Number comparison

    5  3; // false 3  3; // true 3  5; // true 

    Number to BigInt comparison

    5n  3; // false 3  3n; // true 3  5n; // true 

    Comparing Boolean, null, undefined, NaN

    true  false; // false true  true; // true false  true; // true true  0; // false true  1; // true null  0; // true 1  null; // false undefined  3; // false 3  undefined; // false 3  NaN; // false NaN  3; // false 

    Specifications

    Browser compatibility

    BCD tables only load in the browser

    See also

    Found a content problem with this page?

    This page was last modified on Mar 30, 2023 by MDN contributors.

    Your blueprint for a better internet.

    Источник

    4 ways to compare strings in JavaScript

    In this short JS tutorial, you’ll learn how to compare strings and see code examples.

    Strict equality

    To determine whether the strings are equal, you can use the strict equality operator === . It returns false if the strings are different and true , if they’re the same

    const s1 = 'learn'; const s2 = 'today'; console.log(s1 === 'learn'); // true console.log(s1 === s2); // false 

    Comparing the strings using strict equality === always analyzes the case of the letters, meaning that capital letters are different from the small ones.

    const s1 = 'javascript'; const s2 = 'Javascript'; console.log(s1 === s2); // false 

    Case-insensitive string comparison

    If you want to do case insensitive comparison of the strings in JavaScript, you can turn both strings to lowercase and compare them using strict equality operator afterwards.

    const s1 = 'javascript'; const s2 = 'Javascript'; console.log(s1.toLowerCase() === s2.toLowerCase()); // true 

    Comparing the length of JavaScript strings

    If you need to find which of two strings is longer, then the operators “greater than” and “lower than” won’t suit you well. They compare the characters of a string in alphanumeric order one by one and consider the length of the strings in the very end.

    const s1 = 'javascript'; const s2 = 'node.js'; console.log(s1 > s2); // false 

    In JS, every string has the length property. By comparing the value of this property in different strings, we’ll get to know which of them is longer.

    const s1 = 'javascript'; const s2 = 'node.js'; console.log(s1.length > s2.length); // true 

    Check if a string contains another string

    To check if one string is a substring of another one in JavaScript, there’s a built-in function includes . Remember, the function contains exists in Java, but in JavaScript, it’s deprecated and replaced by includes .

    const s1 = 'javascript'; const s2 = 'python'; console.log(s1.includes('script')); // true console.log(s2.includes('script')); // false console.log(s1.contains('java')) // ERROR! .contains is not a function 

    Источник

    Читайте также:  Java extends что значит
Оцените статью