Skip to content
Advertisement

Why use !== when you can use ===?

I could simply vice versa the code to be executed by the if and else. And === is more easier to use compared to !== so why is the not equal operator used?

function equality( number ){
    if ( number === 7 ){
        return "it's equal"
    } else { return "not equal"}
}
 console.log(equality(7))

function nonEquality( number ){
    if ( number !== 7 ){
        return "it's not equal"
    } else { return "it's equal"}
}
console.log(nonEquality(7));

Advertisement

Answer

You cannot use conditional statements to convert the operators for most common cases. In your example, it would be easy and readable to convert the non-equal operator to equal operator by using if-else. But what if a condition like this?

if(i > 4 && i < 10 && i !== 8)

Do you want to apply converting logic here to replace the non-equal operator? The more you convert, then more complicated and unreadable your code is.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement