This time I will bring you how to scientifically use the comparison operator in js, and what are the precautions for scientifically using the comparison operator in js. The following is a practical case, one Get up and take a look.
Comparison operators
The most common types of relational operators are comparison operators, which are used to determine the relative order of two values. The comparison operators are:
Less than (<) The < operator evaluates to true if the first operand is less than the second operand, and evaluates to false. The greater than (>)> operator evaluates to true
Evaluates to false if the first operand is greater than the second operand. Less than or equal to (< =)) The < = operator evaluates to true
if its first operand is less than or equal to its second operand; otherwise it evaluates to false. Greater than or equal to (=)) > = operator evaluates to true
if its first operand is greater than or equal to its second operand; otherwise it evaluates to false. The operands of these comparison operators can be of any type. However, comparisons are only possible on numbers and strings, so operands that are not numbers or strings are converted. Comparisons and conversions are as follows:
If both operands are numbers, or both numbers are converted to numbers, they are compared numerically.
If both operands are strings or converted to strings, they are compared as strings.
If one operand is or converts to a string and one is or converts to a number, the operator attempts to convert the string to a number and performs a numeric comparison. If the string does not represent a number, it is converted to
and the comparison is false. (In
JavaScript 1.1, converting a string to a number would cause an error instead of yieldingnan.)
If an object can be converted to a number or string, JavaScript performs the numeric conversion. For example, this means performing a numerical comparison on date objects, comparing whether two dates are earlier than the other.
These operators always return false if their operands cannot be successfully converted to numbers or strings. Converting to a comparison operator always produces false if both operands are OR.
Please remember
String comparison is done on a strict character-by-character basis, using the numerical value of each character in the Unicode encoding. While the Unicode standard allows equivalent strings to be encoded using different character sequences in some cases, JavaScript comparison operators do not detect these encoding differences; they assume that all strings are represented in canonical form. Note in particular that string comparisons are case-sensitive, and in Unicode encodings (at least for the ASCII subset), all uppercase letters are smaller than "lowercase letters". This rule can lead to confusing results if you don't expect it. For example, the string "zoo" is less than the string "aardvark" according to the < operator. For a more powerful string comparison algorithm, see localecompare() for strings. Methods also take into account region-specific definitions arranged in alphabetical order. For case-insensitive comparisons, you must first string all lowercase or uppercase strings. toLowerCase()
or String.toUpperCase(). The < = (less than or equal to) and > = (greater than or equal to) operators do not rely on equality or identification operators to determine whether two values are equal. Instead, the less than or equal operator is simply defined as "not greater than" and the greater than or equal operator is defined as "not less than". In this case, all four comparison operators return false. The operator expects the left-hand operand to be convertible to a string. A right operand is expected to be an object (or array). It evaluates to true if the value on the left is the name of a property of the object. For example:
VaR point = { x: 1, Y: 1 }; // Define an objectvar has_x_coord = "X" point; // Evaluate truevar has_y_coord = "Y" point; // Evaluate truevar has_z_coord = "Z" point; // false; is not a 3D pointvar ts = "description" point; // inherited property; true
instanceof operator
This is the operator expecting the left hand operand is an object and the right operand is the name of an object of a class. The operator evaluates to true
If the object on the left is an instance of the class on the right, the calculation result is false
I believe you have mastered the method after reading the case in this article. Please pay attention for more exciting things. Other related articles on php Chinese website!
Related reading:
Integration of daily commonly used functions in JS
How to make a decision tree in javascript
The above is the detailed content of How to use comparison operators scientifically in js. For more information, please follow other related articles on the PHP Chinese website!