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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
if (favoritePhrase) {
console.log(“This string doesn’t seem to be empty.”);//prints this log
} else {
console.log(‘This string is definitely empty.’);
}
why empty array consider as a truthy value even it has no values in it?
To check if an array is empty use the length property.
let favoritePhrase = [];
if (favoritePhrase.length === 0) // Returns true for an empty array.
if(favoritePhrase.length != 0) // Returns false for an empty array.
Not really weird when we consider a space is a printable character, a string with length 1. Only the empty string is evaluated as falsy, all others are truthy.
In your first snippet, numberOfApples has a bool value of false because 0 has been assigned to numberOfApples. In JavaScript 0 is falsy.
In your second snippet, you are doing a comparison. Is numberOfApples equal to 0, or perhaps more appropriately, the value assigned to numberOfApples is equal to 0 (true or false)? The expression is true.
Ok, let me see if I actually really do have this concept down.
const n=0;
console.log(n==false); returns true because 0 is a false value. Is that accurate?
console.log(n===false); returns false because ‘===’ is strictly for comparing exact values and these values return false because they are not the same type. Please correct me if I’m wrong, but the way I am understanding this is, in this case ‘n’ and ‘0’ are actually two different object types… one is a string, the other, a number. Therefore, false. Am I way off the mark?
0 evaluates as a falsy truth value. In the expression, because loose typing is applied, the value is coerced to a boolean to match the other type. Zero is falsy so it gets coerced to, false and the equality is then true.
Loose typing and coercion are a part of JavaScript (ES) that many C and Java programmers complain about. There is good reason to complain, but also good reason to get a handle on loose typing and coercion. It is not a thing to be treated lightly, nor a thing to hate out of the box. It has its uses.
Strict typing, on the other hand has no ambiguities. It is what it is. Type for type. Identity. In truth, this is the least confusing relationship to get to understand.
a === b
if, and only if both a and b have the same datatype; and, a and b are exactly alike.
No confusion there. The loose typing, though. Another question. Loose typing must be purpose driven, and reasonably vetted to be of much production value. Strict typing, on the other hand leaves no question.