In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to judge the inequality of JavaScript. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
JavaScript methods to judge whether they are not equal: 1, use the "= =" or "! =" operator to compare whether the values of two operands are equal; 2, use the "=" or "! =" operator to compare whether the values of the two operands are equal and detect whether they are of the same type.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
JavaScript judges whether they are equal or not
In JavaScript, you can use the = =,! = = operators to determine whether they are equal or not
Equivalence detection operator description = (equal) compare whether the values of two operands are equal! = (do not want to wait) compare the values of two operands are not equal = = (congruent) compare whether the values of two operands are equal, at the same time detect whether their types are the same! = (not equal) compare whether the values of the two operands are not equal, and detect whether their types are not the same.
In the equality operation, we should pay attention to the following problems:
If the Operand is a Boolean, it is converted to a numeric value first, where false is converted to 0 and true to 1.
If one Operand is a string and the other is a number, try to convert the string to a number first.
If one Operand is a string and the other is an object, try to convert the object to a string first.
If one Operand is a number and the other is an object, try to convert the object to a number first.
If both operands are objects, the reference address is compared. If the reference address is the same, it is equal; otherwise it is not equal.
Example 1
The following is an equality comparison of special operands.
Console.log ("1" = = 1); / / returns true. The string is converted to the number console.log (true = = 1); / / returns true. True is converted to 1console.log (false = = 0); / / true is returned. False is converted to 0console.log (null = = 0); / / returns falseconsole.log (undefined = = 0); / / returns falseconsole.log (undefined = = null); / / returns trueconsole.log (NaN = = "NaN"); / / returns falseconsole.log (NaN = = 1); / / returns falseconsole.log (NaN = = NaN); / / returns falseconsole.log (NaN! = NaN); / / returns true
NaN is not equal to any value, including itself. Null and undefined values are equal, but they are different types of data. Null and undefined are not allowed to be converted to other types of values in equality comparisons.
Example 2
The values of the following two variables are equal.
Var a = "abc" + "d"; var b = "a" + "bcd"; console.log (a = = b); / / return true
The equality comparison of numeric value and Boolean value is more efficient, while the string needs to be compared character by character, and the equality comparison operation efficiency is relatively low.
In congruent operations, we should pay attention to the following problems:
If both operands are simple values, they are equal as long as the values are equal and the types are the same.
If one Operand is a simple value and the other is a compound object, it is not congruent.
If both operands are compound objects, compare whether the reference address is the same.
Example 3
The following is an congruent comparison of special operands.
Console.log (null = undefined); / / return falseconsole.log (0 = = "0"); / / return falseconsole.log (0 = false); / / return false
Example 4
The following is a comparison of two objects that return true because they both refer to the same address.
Var a = {}; var b = a share console.log (a = b); / / return true
Although the following two objects have the same structure, but their addresses are different, they are not equal.
Var a = {}; var b = {}; console.log (a = b); / / return false
Example 5
For compound objects, the referenced address is mainly compared, not the value of the object.
Var a = new String ("abcd); / / define string" abcd "object var b = new String (" abcd); / / define string "abcd" object console.log (a = b); / / return falseconsole.log (a = = b); / / return false
In the above example, the values of the two objects are equal, but the reference addresses are different, so they do not want to wait or equal. Therefore, for composite objects, the results of the equality = and congruence = = operations are the same.
Example 6
For simple values, as long as the type is the same and the values are equal, they are congruent, regardless of the process of expression operation or the reference address of the variable.
Var a = "1" + 1x var b = "11"; console.log (a = b); / / return true
Example 7
The expression (a > b | | astatb) is not exactly equal to the expression (a > = b).
Var a = 1 * * var b = 2 * * console.log ((a > b | | a = = b) = (a > = b)); / / returns true, which seems to be equal at this time
If you assign values null and undefined to variables an and b respectively, the value false is returned, indicating that the two expressions are not exactly equivalent.
Var a = null;var b = undefined;console.log ((a > b | | a = = b) = (a > = b)); / / returns false. The value of the expression is not equal.
Because null = = undefined equals true, the return value of the expression (a > b | | a = = b) is true, but the return value of the expression null > = undefined is false.
This is the end of the article on "how to judge whether JavaScript is equal or not". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.