FAQ: Conditional Statements - Truthy and Falsy Assignment

When a single character or group of characters appear without quotes it means they are identifiers. We know them as variables, the labels we assign values to so they can be referenced.

a = 6
b = 7
meaning_of_life = a * b

When we wish to have the program treat a sequence of characters as textual data, or strings then we wrap the character string in quotes.

console.log(`The meaning of life is, ${meaning_of_life}.`)

Above we use back-ticks as string delimiters for what is known as a template literal, a special syntax that accepts interpolated expressions (values) directly in the string without the need for concatenation.

abc    =>  identifier (variable)
"abc"  =>  character string
'abc'  =>  character string (same as above)
`abc`  =>  character string (with permitted interpolation)
4 Likes

Thank you for this explanation!! It’s very clear now.

1 Like
let tool = 'marker';
let writingUtensil = tool && 'pen'; // returns The pen is mightier than the sword.

Why when I sub && for || does it return pen?

It has to do with 'pen' being a a truthy object. In an OR expression, a truthy first operand short-circuits. In an AND expression if the first operand is falsy, then it short-circuits on that. If the first is truthy, then it defaults to the second.

 > 'aaa' && ''
<- ""
 > '' && 'aaa'
<- ""
 > 'aaa' && 'bbb'
<- "bbb"
1 Like

confused
But
let tool = 'marker';
comes first. Then…
let writingUtensil = tool && 'pen';
In which case, using && should it not return,
The marker is mightier than the sword.
?

No, it should yield pen. When the first operand is truthy, the second operand is evaluated and no matter whether truthy or falsy, that is the operand that will be yielded. When the first operand is falsy, the expression is short-circuited and the second operand is not evaluated so the first value is yielded.

OK, I’ll just take your word for it. Thank you.
javascript is bending my mind

Have you experimented with it? You don’t have to take my word. See what happens in various scenarios with hands-on trials. Be sure to follow this up with some extra reading about logical expressions in JavaScript.

I misspoke. I believe you 100%. I just don’t understand, if there are two operands and they’re both truthy, why does the code evaluate to the second and not the first operand. It doesn’t make sense to me. If the first (presumptive primary) assignment is truthy, it seems the first operand should be evaluated. (sorry for any weird or poor-use of terminology – I’m super new to all this)

Because when the first operand is truthy, the second operand is evaluated. Recall we are not using if or while to act upon the evaluation. Those both yield a boolean. Written as plain expressions as we have above, the truthiness is evaluated and one or the other operand is yielded, not a boolean.

1 Like

I understand that it IS this way, but can you explain why? That’s what I’m trying to figure out at this point.

1 Like

Because it is a logical expression that may be partially or completely evaluated, depending on the truthiness of the first operand. If we ignored the second operand we would not need &&. As stated earlier, play around with this in several scenarios and see the outcomes.

1 Like
 > true && true
<- true
 > true && false
<- false
 > false && true   // short-circuit
<- false

Interestingly when I ran this exact code in the Chrome console, it returned what I had originally expected. The marker truly is mightier where Chrome is concerned. So this example completely contradicts Codecademy.

more confused than ever

Well, that would be expected since you logged tool and not writingutensil.

marker-v-pen

You’re right - my mistake.

So when I change the variable reference to the correct one in the template literal, here’s what happens.

let tool = 'marker';
let writingUtensil = tool && 'pen';
console.log(`The ${writingUtensil} is mightier than the sword.`)
The pen is mightier than the sword.

let tool = 'marker';
let writingUtensil = tool || 'pen';
console.log(`The ${writingUtensil} is mightier than the sword.`)
The marker is mightier than the sword.

1 Like

For s&g, set tool to null and run the OR again.

1 Like
let tool = null;
let writingUtensil = tool || 'pen';
console.log(`The ${writingUtensil} is mightier than the sword.`)

The pen is mightier than the sword.
1 Like

Beginning to make some sense, now?

yes, what’s weird to me at this point is that javascript allows this declaration

let writingUtensil = tool && 'pen';

Isn’t this assigning a variable two values, effectively (or creating an array)? That seems like it shouldn’t “work.” IOW it seems like it should return an error.