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?
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.
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…
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!
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.
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?
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?
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.
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.`);