![]() Are you saying that he doesn’t understand the language? When I see = throughout code it just shows me that the developer doesn’t have a competent understanding of the language.Īnd yet, Douglas Crockford recommended triple equals for years as a best practice. I even recently saw a young coder who read the supposed ‘best practice’ trying to correct a coder who correctly used = which was again embarrassing to anyone watching who knew the language. ![]() Iv been coding for 35 years btw and I see complete misuse and even breaking use of = every day, and ofc it embarrassingly reveals is that the coder doesn’t understand what they are doing. If you don’t believe me why not you study = and = and start asking yourself what is most appropriate per case rather than just believing the incorrect ‘best practice’ advise. I propose that if you truly know how = and = both work and use them appropriately then you will very rarely need to use = and you will also find that overuse of it will cause you more problems than under-use. If you just wanna test if something is truthy or falsey youll usually just want to test it verbatim or !negated like if (!val) vale= 1 // ofc being careful of string ‘0’ etc. If you have 2 values that may be null, false, undefined, ”, and even perhaps 0 youll usually want = because in practice usually these values are effectively the same (ie not set or empty). If ur working with numbers and your values may be strings (ie keys parsed from json for example or from an input element) then you’ll want to use =, like 2 = ‘2’ If you know how each of = and = work you’ll use each one appropriately and get the most out of the language. In fact in most cases when testing values in js you want =Īnyone who says otherwise simply doesn’t understand the language operators. The recommendation to always use = is bad advise. I think the best summary comes from Zakas again, where, after recommending always using strict equals, he says: “This helps to maintain data type integrity throughout your code.” While researching this article, I learned a few things about this concept myself. If we change to double-equals, we get the correct results:Īs mentioned, you’ve probably already used triple-equals pretty exclusively. Instead, they’re false - because of type coercion. Notice now that the desired result in each case should be “true”. Here are the same examples from above, this time expressed with the != operator: double-equals, you’re using double-equals vs. Except this time, instead of triple-equals vs. When doing a not-equals-to expression, the same rules apply. This means that, using triple-equals, all the examples from above will produce the correct results: You’re ensuring the values are ‘strictly equal’ or ‘identically equal’. So whenever you use triple-equals, you’re doing an exact comparison of the actual values. The triple-equals operator, as you’ve probably figured out by now, never does type coercion. In light of this, most JavaScript experts recommend always using the triple-equals operator, and never using double-equals. Here are a few examples:Īlthough this can initially feel like a good thing (because the browser seems to be doing you a favour), it can cause problems. Similarly, if you compare true or false with a number, the true or false value will be converted to 1 or 0, respectively. So if you try comparing (for example) a string with a number, the browser will try to convert the string into a number before doing the comparison. These types are: Numbers, strings, Booleans, functions, and objects. But there’s one important catch: If the comparison being made is between two different “types” of values, type coercion will occur.Įach JavaScript value belongs to a specific “type”. ![]() In a comparison using the double-equals operator, the result will return true if the two things being compared are equal. ![]() JavaScript beginners who try to adhere to best practices may be using triple-equals and not double-equals, but might not fully understand what the difference is or why it’s important to stick to triple-equals. The difference between those two examples is that the second example uses the triple-equals operator, also called “strict equals” or “identically equal”. Or, if you’re conforming to best practices, this: In other words, throughout your scripts you’ll probably have lines resembling this: “Determining whether two variables are equivalent is one of the most important operations in programming.” That’s according to Nicholas Zakas in his book JavaScript for Web Developers. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |