In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to judge the equality of variables by javascript". In daily operation, I believe that many people have doubts about how to judge the equality of variables by javascript. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to judge the equality of variables by javascript". Next, please follow the editor to study!
1. = = is the conversion type comparison operator, and = = is the strict comparison operator. In the case of the same data type, using = = is generally not a problem.
Let num = 5 num2 = 5; num = = num2;//truenum = = num2;//true
2. Array toString method, when the front end passes parameters to the back end, the back end sometimes requires multiple parameters, separated, the Array toString method is more useful, this method can also be used for array comparison.
Let arr = [1JE3, 5, 5, 7, 7]; let arr2 = [1, 1, 3, 5, 5, 7, 7, and 9]; arr.toString () = = arr2.toString (); / / true "1, 3, 5, 5, 7, and 9"
3. The isEqual function can judge most complex objects (except those with functions in the object).
Function isObject (obj) {return typeof obj = = 'object' & & obj! = = null} function isEqual (a B) {if (a = = b) return true var isObjectA = isObject (a) var isObjectB = isObject (b) if (isObjectA & & isObjectB) {try {var isArrayA = Array.isArray (a) var isArrayB = Array.isArray (b) if (isArrayA & & isArrayB) {/ a / b are arrays return a.length = b.length & & a.every ((el, index) = > isEqual (el) B [index])} else if (an instanceof Date & & b instanceof Date) {/ / a b are Date objects return a.getTime () = b.getTime ()} else if (! isArrayA & &! isArrayB) {/ / at this time a b are pure objects var keyA = Object.keys (a) var keyB = Object.keys (b) return keyA.length = keyB.length & & KeyA.every (key = > isEqual) B [key])} else {return false}} catch (e) {console.log (e) return false}} else if (! isObjectA & &! isObjectB) {/ / a b may be string A kind of return String (a) in number,boolean,undefined (a) = = String (b)} else {return false}} at this point, the study of "how to judge the equality of variables by javascript" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.