1

I've been studying JavaScript for a couple months, and I have some questions.

Say we have some if statements like this:

if (something)

if (!something)

From what I have looked up, the first statement checks whether something evaluates to true, whereas the second one checks for whether the condition is not true(I recall reading the ! coerces it into boolean and then 'inverses' it).

However, I'd like to know what are these statements' direct equivalents when not "shortened". I'd like to understand code like this more in depth before really using it.

Is if (something) the same as if (something == true)? Or even if (something === true)?

And what would be the "non-shortened" (for my lack of a better term) direct equivalent of if (!something)?

Thank you for any help.

Nafoie
  • 13
  • 2
  • 1
    `if (something)` is more like `if (Boolean(something))`, because `if` coerces the expression to boolean. – Nina Scholz Mar 15 '20 at 15:56
  • `if (Boolean(condition) === false)` – Keith Mar 15 '20 at 15:58
  • 1
    It's a mix of [type coersion](https://stackoverflow.com/questions/19915688/what-exactly-is-type-coercion-in-javascript), [Truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) and [Falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) values and [Which equals operator (== vs ===) should be used in JavaScript comparisons?](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) – Andreas Mar 15 '20 at 16:02
  • Exactly @Andreas .. it deals with truthy falsy. I doubt if `if` coerces actually. A *boolean expression* does the *coercing* in all actuality. So anything written as a boolean expression would "coerce", say a `while` or other kind of loop. Or just a direct variable declaration. The "instance" is at the expression level. The boolean operators can reduce to boolean values. But really no "coercion" is happening on the function level. – GetSet Mar 15 '20 at 16:03

1 Answers1

3

Part 1:

When we use if(something) javascript will check if it is a 'Truthy' or 'Falsy' value.

So if we say

if(1) {
    alert("Yes, it's true...");
}

javscript will check if 1 is a "truthy" value and it is. Everything that is not a falsy value is truthy by extension.

Falsy values are: 0, 0n, null, undefined, false, NaN, and the empty string “”. So if we directly put any of the above into a parenthesis it will return false.

e.g. if (null) ... the if clause won't be executed because null is falsy.

Part 2:

When we use if(something == true), we check if one value is equal to another value. If the types are not the same, since we're using == coercion will happen.

What is this about 'coercion'? If we say if('1' == 1) this will execute the if clause as it returns true. Javascript will convert 1(number) to '1'(string) in this case.

If we want to be strict and say "I only want true if they're both numbers, I don't care if 1 is a string" then we use if(1 === '1') and this will not execute the if clause(because the condition returns false).

So if we say if(false == false) this will return true because false is equal to false, and the if clause will be executed.

Note the difference if we said if(false) or if('') the if statement will not be executed.

Next, if we want to use negation, we have to put the ! in front of the boolean we're negating, regardless of whether it's part of an if condition or... There is no way around it.

However, there is a shorter version of this:

var a = 3;
var b = 2;

if(a===b) {
    alert('A is equal to B');
} else {
    alert('B is not equal to A');
}

This is considered shorter(ternary operator): Note that if we want to use more than one statement, we should use the if else

a = 3;
b = 2;

a===b ? alert('A is equal to B') : alert('B is not equal to A');
BroDev
  • 582
  • 1
  • 4
  • 14