if(condition) is equivalent to a condition with == or ===? And also if(! Condition) is this!=Or! ==?

5 Answers 5

[[ThunderCat]] and[[Lynn]] noticed quite true, the condition if(someVar) does not work when someVar is true, but when it is not null, 0,"", void 0, or false.

About undefined costing something to clarify: if a variable has not been declared anywhere, then you will generally get a ReferenceError exception, but if it is declared and the variable is set to undefined, or you are accessing the missing property of the object, then it can roll, for this, I want to immediately draw attention[[joeberetta]] to the inadequacy of the article cited by him, since from the start they teach a poor comparison pattern with undefined, forgetting to mention that the safe way of checking for undefined is typeof someVar ==='undefined'

You should also understand that checking if in the if statement the variable value and the value of the expression result is not the same thing and the one with the other, it is not that incomparable, but not even equivalent.The only case where you can compare things are if both operands are cast to a Boolean type, in other words, we can talk about equivalence only in the case of if(!! someVarr === true) or if(Boolean(someVar)=== true), and it doesn't even matter what you use, ==, or === and even in this case, speaking about equivalence, we pull it by the ears by and large do not adequately match the expression's value to the value peremennoy.Nauchites split mind these two concepts, and you will be happy :)
  • Probably they did not quite understand me, because I did not indicate that I was comparing if(variable) and if(variable === true/false).The variable during the program changes its value to either true or false.But I already saw the answer , thank) – Easy Echidna May 18 '19 at 12:56
the most obvious: if(1)!=if(1 === true) at least, just look at what satisfies the execution of eif in the mask script.
No, neither one nor the other.For example, condition=5
Habr.The right question, the right answer!
I will try to explain it easier than what is written above.

[[Dvornik33]], the if statement works like this - with what is written in brackets, the cast is performed to boolean, that is, to True or False.If the brackets are written expression, it is executed and its result is converted to boolean.

At the same time, it is not at all necessary that the expression in brackets be using == or ===, for example:
 if(3 * 4) {

Expressions with == or === are just a kind of expressions that return a logical type.You can write it like this:
const b=3 === 3;
True to variable b will be written

Logical expressions are often used with the if construct because it is convenient — a logical expression returns the result of a boolean type, and if works with a boolean type value.

c == and === is even simpler: when == in a logical expression, the right and left parts of an expression are first reduced to a number and then compared.When ===, the comparison is performed without reduction to a number.Since the explicit is better than the implicit, It is recommended to use === to avoid implicit coercion to a number.

how different types are reduced to boolean or a number, I hope you read somewhere yourself.

Is the if(condition) condition equivalent to the if(condition === true) condition

if your <condition> is an expression that returns a boolean type, then yes, is equivalent.