FAQ: Conditional Statements - Truthy and Falsy Assignment

It only evaluates to true in a conditional statement like an if or while.

if (A && B)

JS sees a boolean.

A && B

is evaluated completely different by JS. It employs short-circuiting on falsy operands.

A = 0
B = 1

A && B    =>  0

The yield of the expression is the value of the first falsy operand, in this case A, so 0. B is not evaluated.

Now if we switch the operands around,

B && A    =>  0

we still get 0 since the first operand didn’t short-circuit so the second operand is the yield.

'' && 'pen'

Note that the short-circuit value is the empty string. So,

The  is mightier than the sword.

Now when tool is not an empty string it is truthy so the second operand is what comes out the end.

The pen is mightier than the sword.
1 Like

Excuse me, sorry but I have been stuck on this for an hour. I am not stuck because I don’t understand but because there is a glitch of some kind. I have refreshed exited and came back and it still hasn’t changed. What had happened is that this keeps showing up every time I refresh instead of the code that was supposed to come up. I had to go and click on solution in the hopes that it would fix itself but that hasn’t happened. What should I do?

I mean can we store a condition into a variable, and then use that variable instead?
What is bitwise operator by the way?

Hello there, In what ways can conditionals be modeled by using electronic circuits?

One could Google this for a list of operators. They are used with binary numbers. The common ones are,

  • & (AND)
  • | (OR)
  • ^ (XOR)
  • << (left-shift)
  • (right-shift)

Left shift is the same as multiplying by 2, right shift is the same as floor dividing by 2. The logical operators are for comparing bits in one value with bits in another.

AND

1 1 1 1
1 0 0 1

which will give 9 (1001), since those bits are the same.

OR

1 1 1 1
1 0 0 1

which will give 15 (1111), since at least one of the bits is set in all four comparisons.

XOR

1 1 1 1
1 0 0 1

which will give 6 (110), since those bits are exclusive.

1 Like

Like in the example below, I have used a boolean variable instead of writing the condition itself. In my view this might enhance the code readability and may make it easier to understand. Is this possible to do that all the time with conditionals and store the condition in a meaningful boolian variable the same as this example?

let number = 110; let isEven = (number % 2 === 0); if(isEven){//if(number % 2 === 0) console.log("The given nubmer is even"); } else { console.log("The number is odd"); }

isEven is a boolean, not a stored conditional. We would need a function to store a conditional.

const isEven = function (x) {
    return x % 2 === 0
}
if (isEven(number)) {
    console.log(...)
} else {
    console.log(...)
}
1 Like

Where do we get solution of these things?? come on i dont understand anything and no resolution ? What a ■■■■

Hi, Could you please tell me? What is the exact purpose of using Truthy and Falsy Assignment.

I think it is mostly about absolute or implied composition. We can describe ‘nothing’ in several forms. Zero, an empty string, null, undefined, etc.
Those values can be given as falsy. All others are truthy.

It’s a singular expression evaluation. That’s the long and short of it.

Thank you so much! well appreciated the answer.

Great exercise!
But the most interesting thing happens if we try to use the logical operator && instead of | | ! See what value the computer assigns to the variable!

let tool = 'lightsaber';

let writingUtensil = tool && 'pen';

console.log(`The ${writingUtensil} is mightier than the sword.`);
// Prints 'The pen is mightier than the sword.'

tool variable is truthy. So why does the computer ignore the first value and assign the second one?
p.s. I was just wondering what the computer would do if we offered it two values for a variable at once :slight_smile:

Read up on the concept of short circuiting as it applies to && and ||.

AND short-circuits on, false or falsy; OR short-circuits on, true, or truthy.

1 Like

Hi there !
Hope this question was not answered here before, but I did not see anything like it.

So, when I create a new var with a falsy value, is there any difference in how i create the var ?

Like for example:

let var1 = '';
console.log(var1); // prints out: undefined

let var2;
console.log(var2); // prints out nothing

Both variables have falsy values, but are there any pro’s or con’s for each method,
besides that one prints out undefined, while the other prints out nothing ?

let var1 = '';

var1 is declared and defined, so it is not undefined, but when logged will print only a blank line.

let var2;

var2 is declared but not defined, so when it is logged it will print, undefined

Witness:

 > let var1 = ''
<- ''
 > console.log(var1)
                                                  VM111:1 
<- undefined

The last line is the return value of console.log(). Note the blank line above it.

 > let var2;
<- undefined
 > console.log(var2)
undefined                                         VM203:1 
<- undefined
1 Like

Please somebody explains why username is set to '' or empty here. Thank you!

let username = '';

In terms of truthiness it is the equivalent of,

let username;

since both will fail,

if (username) {

}

The difference between the two is that '' is a string object, albeit, the empty string. It has methods inherited both from Object.prototype and from String.prototype; it has a length; it is subscriptable but immutable, meaning we can access its character elements but we cannot change them.

Bottom line, undefined has no properties or methods; '' has both.

1 Like