Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the definition and usage of relational expressions in the JavaScript basic course

伊谢尔伦
Release: 2017-07-19 10:24:53
Original
1270 people have browsed it

Relational operators are used to test the relationship between two values ​​(equality, less than, or "is a property of..."), returning true and false depending on whether the relationship exists. Relational expressions always return a Boolean Value, relational expressions are usually used in if while or for statements to control the execution flow of the program.

iEquality and inequality operators

The "==" and "===" operators are used to compare whether two values ​​are equal. The two operators allow any type of operator. . Returns true if they are equal, otherwise returns false. "===" is also called the strict equality operator (sometimes called the identity operator), which is used to detect whether two operands are strictly equal. The "==" operator is called the equality operator, which is used to check whether two operands are equal. The definition of equality here is loose and allows type conversion.

Javascript supports "=", "==", "===" operators, you should understand the difference between (assignment, equality, identity) operators. And use it with care in programming. To reduce confusion, "=" should be called "get or assign", "==" should be called "equality", and "===" should be called "strict equality".

The "!=" and "!==" operator rules are "==", the negation of the "===" operator, "!" is a Boolean non-operator, we will "!= ", "!==" is called inequality, not strict equality

The comparison of JavaScript objects is a comparison of references, not a comparison of values. The object is equal to itself, but neither the person nor the object is equal. If two objects have the same number of properties, the same property names and values, they are still not equal. Even if the array elements at corresponding positions are equal, the two arrays are also unequal.

The strict equality operator "===" first calculates the value of the operand, and then compares the two values ​​without any type conversion.

If the two value types are not the same, they are not equal
If both values ​​are null or undefined, they are not equal
If both values ​​are Boolean true or false, Then they are equal
If one of the values ​​is NaN, or both values ​​are NaN, then they are not equal. NaN and other values ​​are not equal, including itself.
Two values ​​are equal if they are numbers and equal. If a value is 0 and a value is -0, they are also equal.
If two values ​​are strings and the 16 digits (refer to Chapter 3, Section 2) contained in the corresponding bits are exactly equal, they are equal. If they are different in length or content, they are not equal. Two strings may function exactly the same and display the same characters, but have 16-bit values ​​without encoding. JavaScript does not perform standard conversion to Unicode, so such strings are passed through "===" and "==" The comparison results of operators are also not equal. String.localeCompare() in Part 3 provides another way to compare strings.
Two reference values ​​are equal if they point to the same object, array or function. If they point to different objects, they are not equal, even though both objects have exactly the same properties.
The equality operator "==" is similar to the identity operator, but the equality operator comparison is not strict. If the two numbers are not of the same type, the equality operator attempts some type conversion and then compares them.

If the two operation types are the same, the comparison rules for the equality operator are the same as above. If strictly equal, the comparison results are equal. If they are not strictly equal, the comparison results in unequal.
If the two operation types are different, the "==" equality operator will also consider them equal. Detecting equality will follow the following rules and type conversions:
If one type is null and the other is undefined, they are equal
If one value is a number and the other is a string, first convert the string to a number , and then use the converted value for comparison.
If a value is true, convert it to 1 for comparison. If a value is false, convert it to 0 for comparison.
If one value is an object and the other value is a number or string, use the conversion rules of the method in Chapter 3, Section 8, Subsection 3 to convert the object into a primitive value, and then compare. The object is converted to a primitive value through the toString() method or the valueOf() method. The built-in classes in the core of the JavaScript language first try to use valueOf() and then try to use toString(). Except for the date class, the date class can only be converted through toString(). Objects that are not part of the core JavaScript language are converted to primitive values ​​using methods defined in the implementation.
Comparisons between other different types are not equal
Here is a small example of equality

"1" == true
The result of this expression is true, which indicates that it is completely Values ​​of different types compare equal. The boolean true is first converted to the number 1 before the comparison is performed. Next, the string "1" will also be converted to the number 1, because the values ​​of the two numbers are equal, so the result is true.

ii. Comparison operator

Less than (<)

If the first operand is less than the second operand, the "<" operation result is true, otherwise it is false

Less than or equal to (<=)

Greater than (>)

Greater than or equal to (>=)

....(not detailing the meaning)

The operands of comparison operators may be of any type. However, only numbers and strings can actually perform comparison operators, so any operands that are not numbers or strings will undergo type conversion. The type conversion rules are as follows:

If the operand is an object, it is converted to a primitive value according to the conversion rules described in Chapter 3, Section 8, Subsection 3: If valueOf() returns a primitive value, then use this primitive directly value. Otherwise the conversion result of toString() is used for comparison.
After converting to original values, if both operands are strings, then the two strings will be compared in alphabetical order. The "alphabetical order" mentioned here is the composition of the two The index order of the 16-bit Unicode characters of the string.
After the object is converted to a primitive value, if at least one operand is not a string, both operands will be compared numerically. 0 and -0 are equal. Infinty is larger than any other number (except infinty itself), and -infinty is smaller than any number (except itself.) If an operand (or converted) is NaN, the comparison operator always returns false
For For numeric and string operators, the plus operator behaves differently than the comparison operator. The former prefers strings and performs string concatenation if one of its operands is a string. Comparison operators prefer numbers only when both operands are strings. String comparison will be performed.

1 + 2 //=>3 addition, the result is 3
"1" + "2" //String concatenation, the result is "12"
"1" + 2 / /String concatenation, 2 is converted to "2", the result is "12"
11 < 3 //Number comparison, the result is true
"11" < "3" //String comparison, the result is true
"11" < 3 //Comparison of numbers, "11" is converted to 11, the result is true
"one" < 3 //Comparison of numbers, "one" is converted to NaN, the result is false

Finally, it should be noted that the "<=" and ">=" operators do not rely on the equality operator and strict equality comparison rules when determining equality. In contrast, the less than or equal operator is simply "not greater than" and the greater than or equal operator is simply "not less than". There is only one exception. When one of the operands (after conversion) is NaN, all four comparison operators will return fasle.

iii.in operator

in operator Hopefully its left operand is a string or can be converted to a string, and its right-hand side is expected to be an object. If the object on the right has a property name named the left operand value, then the expression returns true. For example,

var point = {
     x: 1,
     y: 1
   } //定义一个对象
   "x" in point //=>true 对象有一个名为x的属性
   "z" in point //=>false 对象无名为z的属性
   "toString" in point // =>true 对象继承了toString方法
   var data = [7, 8, 8]
            "0" in data //=>true 数组包含0
             1 in data //=>true 数字转换为字符串
             3 in data //=>fase 没有索引为3的元素
Copy after login

iiii.instanceof operator

instanceof operator expects the left operator to be a Object, the right operand indicates the class of the object. If the object on the left is an instance of the class on the right, the expression returns true; it is responsible for returning false. Chapter 9 will talk about it. Classes of JavaScript objects are defined by initializing their constructors. In this case, the right operand of instanceof should be a function. For example:

var d = new Date(); //构造一个新对象
  d instanceof Date; //计算结果为true, d是Date() 创建的
  d instanceof Object //计算结果为true ,所有的对象都是Object的实例
  d instanceof Number //计算结果为 false,d不是一个Number对象
  var a = [1,2,3] //数组直接量创建数组
  a instanceof Array //计算结果true a为数组
  a instanceof Object //true 所有的数组都是对象
  a instanceof RegExp //fasle 数组不是正则表达式
Copy after login

It should be noted that all objects are instances of Object. When checking whether an object is an instance of a class through instanceof, this judgment is also called "superclass" detection. If the operation object on the left side of instanceof is not an object, instanceof returns false. If the right-hand operation is not a function, an exception of typewrong is thrown.

In order to understand how the instanceof operator works, you must first understand the "prototype chain". The prototype chain, as the inheritance mechanism of JavaScript, will be described in detail in Chapter 6, Section 2, Section 2.

In order to calculate the expression o instanceof f, JavaScript pen first calculates f.prototyoe, and then queries o in the prototype chain. If found, then o is an instance of f (or the parent class of f), then Return true. Otherwise false

The above is the detailed content of Detailed explanation of the definition and usage of relational expressions in the JavaScript basic course. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!