FAQ: Conditional Statements - Truthy and Falsy Assignment

This community-built FAQ covers the “Truthy and Falsy Assignment” exercise from the lesson “Conditional Statements”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Truthy and Falsy Assignment

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

This exercise should clarify you won’t be using the if blocks.

I mean, all the exercises had if, this one even shows an example but doesn’t clarify what to use or not.

Anyway my reading comprenhension is bad so i don’t know if it’s me lol.

1 Like

I was not able to do this exercise correctly and with the solution i didn’t undersand, so maybe Codeacademy staff could help clarifying it

the conditional (ternary) operator is just a shorthand (docs), we could write:

if (tool){
    let writingUtensil = tool;
  } else {
    let writingUtensil = 'pen';
  }

or use the conditional operator to shorten this code quite a bit:

let writingUtensil = tool ? tool : 'pen';

which has the following general syntax:

let variable = condition ? true : false;

so if the condition is true, use true/tool else (shortened with :) use false/pen.

Let’s use short-circuit evaluation to assign a value…

The idea of short-circuit evaluation is to use a logical expression as given in the lesson example.

let defaultName = username || 'Stranger';

In the lesson, tool = '', which is an empty string. Empty strings are falsy.

let writingUtensil = tool || 'pen';

OR short-circuits on true, or defaults to the last operand.

The pen is mightier than the sword.

Notice how 'pen' takes the place of tool.

Now set tool to a string of some value.

tool = 'quill';

writingUtensil = tool || 'pen';

The quill is mightier than the sword.
2 Likes

In
let defaultName = username || 'Stranger';
why defaultName is not valuated as true?

‘Stranger’ is truthy, so in any case (username || ‘Stranger’) should be true.

What am I missing? Parenthesis? Or Truthy not always IS True?

It is not evaluated, The assignment is. When username is falsy (meaning empty string, None, or 0) OR skips over it to the next operand and returns that, regardless whether truthy or not.

Read up on short-circuiting and how it works in the case of AND (&&) and OR (||).

1 Like

Thanks, I see how it works, I just wonder why.

My train of thought (and it works exactly this way in some other languages):
username is false
‘Stranger’ is true
false || true equals true
so (username || ‘Strangeer’) should equal true

How does the JS interpreter work with this line of code?
Does it read it from left to right?

If it is declared, but undefined, or it is an empty string, or null, or 0 then it is falsy, not false

username || ‘Stranger’

Uncaught ReferenceError: username is not defined

var username;
username || 'Stranger'

“Stranger”

username = "";
username || 'Stranger'

“Stranger”

username = null;
username || 'Stranger'

“Stranger”

username = 0;
username || 'Stranger'

“Stranger”

username = "Wee Gillis";
username || 'Stranger'

“Wee Gillis”

The expressions are not evaluated as literal booleans, but as pseudo-boolean, truthy or falsy.

console.log(operandA || operandB)

is evaluated from left to right, and if operandA is truthy, it will log out, otherwise operandB will log out.

operandA = 0
operandB = false
console.log(operandA || operandB)
// false
operandA = 1
console.log(operandA || operandB)
// 1

Logging and assigning are evaluated the same way. When it comes to a conditional expression such as if or while or for then the action branch depends on the truthiness of the condition, not the literalness.

false || true
// true
false || 0
// 0
3 Likes

Thanks a lot, mtf!

I mistook the semantics of words truthy and falsy.

Now it’s perfectly clear.

1 Like

I’m confused why the value of writingUtensil defaults to ‘pen’ instead of ‘’. Why is ‘’ falsy?

Because as an empty string it represents nothing substantive, akin to no discernible value. "", '', 0, null, undefined and NaN are all falsy

Its funny how I wrote my code exactly as the solution and was still wrong.

1 Like