FAQ: Conditional Statements - Truthy and Falsy Assignment

Dear members, tutors!

I am studying the JS truthy and falsy assignment. There is one thing I dont get. Maybe I missed something.
Here is the following code from the exercise:

let tool = ‘marker’;

// Use short circuit evaluation to assign writingUtensil variable below:

let writingUtensil= tool || ‘pen’;

console.log(The ${writingUtensil} is mightier than the sword.);

Notice that in the exercise when we declared the writingUtensil= tool || ‘pen’;

we did not use the single or double quote for the tool. Why is that? Isn`t it a string in the value?
or is this how we refer to a variable we declared before?

link:
https://www.codecademy.com/paths/front-end-engineer-career-path/tracks/fecp-javascript-syntax-part-i/modules/fecp-learn-javascript-syntax-conditionals/lessons/control-flow/exercises/truthy-falsy-operators

Thank you!

yes, its a variable.

1 Like

Yes, it is still a string when applied to this OR expression. We see tool but the computer sees 'marker'. The interpreter has taken our variable reference and tracked down the content that it points to, then inserted it into the expression for the compiler to run.

tool (the variable to which we have assigned a string value) is an object reference, only.

Best reply ever 10/10 would recommend.

In the example, it should be made more clear. Can’t assume everyone knows ‘username’ was declared elsewhere.

let defaultName; let username; if (username) { defaultName = username; } else { defaultName = 'Stranger'; } console.log(defaultName);

I agree with your point, but how did adding let username; ensure that the code works? My thinking is that if we don’t assign username a certain value, it is undefined…

Which is correct.

a = 42

The above is undeclared, but defined. It will be hoisted to the top of global scope.

let a;

a is declared, but undefined. It will be hoisted to the top of its parent scope.

let a = 42

a is declared and defined. It will be hoisted to the top of its parent scope.

2 Likes

Ohhh, I see now! It has everything to do with the scope. I have also bookmarked your post so I can review it for later, when I do more practice with scopes. This is very helpful — as always — thank you!

1 Like

This is a bit irrelevent, but out of curiosity the lesson states that “JavaScript assigns the truthy value to a variable if you use the || operator in your assignment”.

What happens if both sides of the || operator are falsy? Does Javascript still assume it is truthy?

When both (all) are falsy, the value of the last operand will be the outcome. OR shirt-circuits on true, to which the outcome will be the value of that operand.

console.log(0 || 0 || 0 || 1 || 0 || 0)
1
2 Likes

So in a case where all operands are falsy, the last operand in sequence is assigned. This results in a falsy outcome in the conditional statement, correct?

1 Like

A falsy outcome in that the operand may still be falsy. If there were no non-zero numbers above, the outcome would be 0.

1 Like

Thank you very much, that clears it up wonderfully!

1 Like

Alright I understand that when you want to evalute something you assign a variable to another variable and then check to see if it is true.(That or I didn’t undertand how it works :P.)
So my question is does the top variable(or the one inside) it take priority ?Or is it just a relationship of a variable inside of a variable?

This is the first practice where let was not defined by an equal sign. in the example of making the username variable falsy.

let username = ‘’;
let defaultName;

Does let defaultName not have to be defined in this since there is a if else condition following?

I am confused as to what the purpose of < let defaultName; > does for the code. I just don’t understand the code and I was wondering if someone out there could break it down for me to explain what each of the codelines is doing. I get that the fact that the string is empty means that there is no username which means that Stranger will be displayed but not why this code does that if that makes sense.

let username = ’ ';
let defaultName;
if (username) { defaultName = username;} else {defaultName = ‘Stranger’;}
console.log(defaultName); // Prints: Stranger

In JavaScript variables can be defined without being declared; be declared without being defined; or, be declared and defined at once.

a = 42;        //  defined, not declared

let a;         //  declared, not defined

let a = 42;    //  declared and defined

The first one is automatically hoisted to the top of global scope no matter where it is written. We don’t want to be in the habit of introducing new variables in that manner. It is not a best practice in JS.

The middle one is useful if we wish a variable to exist in outer scope, while it can go through changes in inner scope, such as the example you gave above. defaultName does not get a value assigned until username is tested, and then the value is assigned accordingly.

The last one is most typical. Flexibility means we can have some say in how our variables come into existence, with the proviso that we do everything in our power to not pollute global scope, and leave a small footprint on memory as the program runs. You’ll learn more once you get into the topic of ‘memory leakage’, but that will be some ways down the road.

3 Likes

Can we write all conditionals with binary operators instead?

Do you mean, bitwise operators?

A condition can be a state (in binary that would be 1 or 0) or it can be an expression such as a comparison, which is boolean (true or false).

Not sure I follow your question. Can you give an example?

Hi there!

My question is about using “AND” (&&) operator. Look at the example below.

Shouldn’t the output be “The TRUE is mightier than the sword” rather than “The PEN is mightier than the sword.”?

Thank you in advance!

let tool = '';
tool = 'marker'
// Use short circuit evaluation to assign  writingUtensil variable below:
let writingUtensil = tool &&'pen'


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