What’s the condition you are testing? If what is false?
the condition is just that, the string "false"
please try it yourself, see what happens
One application could be that if a variable has neen assigned the string “false,” and you want to test whether it has, then whatever code you want executed, would be executed.
if (var = “false”) {
console.log(“false”);
}
no, I mean literally just "false"
or if you want, a varaible with the value of "false"
, no comparisons
(note also that =
is the assignment operator, and var
is not a valid variable name, as it’s reserved)
var myVar = "false";
if (myVar) {
// will this execute?
console.log("Wooohooo!")
}
do you remember this challenge?
just like this, just a value inside the condition
yes, should have been:
if (var === “false”) {
console.log("false);
}
i make that mistake a lot but i totally understand it.
i don’t understand what you mean by “just “false”” though.
I mean that I am showing a thing, and you are going in an other direction
if ("false") { // here there is no comparison operator, just a value, you are changing what I wrote, so you can't see what I am talking about
}
I’m sorry I can’t help you further, it doesn’t seem we are understanding each other
My notes from that challenge state “if a condition is true, code is executed” and “this goes inside a function.”
Thanks for trying. Is there someone else willing to try to help me?
@ILM is asking you to see what the code they posted does, as an example of what they are explaining, not to guess what it does.
Everything except false
, 0
, -0
, 0n
, ""
, null
, undefined
, and NaN
is considered true
by JavaScript in a boolean context.
A boolean context is a specific part of the code where JavaScript will convert something that is considered true to true
and something that is considered false to false
. JavaScript automatically coerces a value that is not literally true
or false
to actual true
or false
.
Something that is considered true is called a “truthy” value. Something that is considered false is called a “falsely” value. To reiterate:
Everything except
false
,0
,-0
,0n
,""
,null
,undefined
, andNaN
is consideredtrue
by JavaScript.
The main boolean context is the head of an if
statement:
if (thisIsTheBooleanContext) {
...
}
And a filter
callback
someArray.filter(function(someValue) {
// This is the Boolean context
});
Thank you for responding but I am a beginner so you’re going to have to slow down with me. I understand that something can be true or false. False values are falsy and some examples of falsy values are 0, false, null. Not sure I understand boolean context, though.
JavaScript provides ways to treat data of one type as data of another type. For example, you may have the character "2"
in a string, but you want to use it like a number. You can evaluate it as a number in order to do arithmetic on it (or vice versa). There are other examples of when you want to translate a value into a different type. One of the most common times this happens is when you want to translate something into a boolean. A boolean is either true
or false
. It is a type with only two possible values. There are specific values that are “translated” (which we call “casting”) to false
; we call them “falsy”. That is the list provided by the challenge. Every other value is cast to true
.
You can explicitly cast a value to a boolean like this:
let boolVal = Boolean(someVal); // if someVal is falsy, boolVal will be false
Values can also be implicitly cast to booleans. This doesn’t change the original value, but it allows JavaScript to use the boolean “translation”. if
conditions are always evaluated as true
or false
if (someVar) {
...
}
// is exactly the same as
if (Boolean(someVar)) {
...
}
I’m trying to get a fresh start today. Can you please delete you post and refrain from posting on this thread? Thank you.
The bit inbetween the round brackets in an if
statement
isn’t that the test condition?
Sure, and it is a boolean context, where JavaScript automatically coerces a value that is not literally true
or false
to actual true
or false
I don’t understand this. Can you please pretend I’m 8 years old and try to explain this concept to me? I really am a beginner.
Anything you put into round brackets at the top of an if
statement JavaScript will convert to the value true
or false
regardless of what it is.
Values that are “falsely” are ones that convert to false
. Values that are “truthy” are ones that convert to true
When JavaScript needs a value to be true
or false
it will attempt to convert it from whatever it is to actual true
or false