JavaScript #11 ~ Equality Operators: == vs. ===
In previous posts, I talked about the importance and functions of logical operators (>, <). But, what if two values are equal to each other. Besides logical operators, there are also equality operators to compare two values that are equal to each other.
First, we have the strict (===) equality operator that compares two or more operands by checking the equality between the values as well as their types. On the other hand, we have the loose (==) equality operator that compares two or more operands by converting ehir value to a common type first and then checking for the equality between them. The strict equality only returns 'true' if the values and the type both match with the other operand. The loose equality allows more leniency between operands and allows various types to return true.
Because of this leniency with loose operators, there are more chances to encounter unexpected bugs and errors within code. As such, developers almost always tend to use the strict equality, due to its type reasonable type handling function.
const age = '18'; if (age === 18) { console.log('You just became an adult :D'); // strict equality if (age == 18) { console.log('You just became an adult :D'); // loose equality
There is also the not equal (!==) operator that checks whether two or more operands are not equal to each other. This method also has the same functionality as the ones mentioned above
const age = '18'; if (age !== 18) { console.log('You are not and adult yet!); }
Author And Source
이 문제에 관하여(JavaScript #11 ~ Equality Operators: == vs. ===), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@hjbaek91/2021.06.01Tue-TIL-Equality-Operators-vs저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)