I am just confused here. We made a let variable called “sale”, with the content of it true, right? But then we changed it to false, how come, if it’s false now, a line it the if function says if(sale), however, i think that means “if false”, but if its false why doesn’t it do the function?
let sale = true;
sale = false;
if(sale) {
console.log('Time to buy!');
}
I understand the basic concept of this, but I am confused as to how the code determines the true or false aspect of it. Here is my code:
let sale = true;
sale = false;
if (sale) {
console.log(‘Time to buy!’);
} else {
console.log(‘Time to wait for a sale.’)
}
Conceptually, if it is “True” then the output will be “Time to buy!”, and if it “False” then the output will be “Time to wait for a sale!” But my question relates to how it knows which is which. Since we set the first variable to True and the second one to False, is that why it connects the first statement with being True and the second one to False? Meaning, is the order of the set variables important?
I had a question regarding the semantics of spaces within JavaScript code:
On the Learning (left) side, part of the code is written as if (false) { but on the JS exercise (right) side, it doesn’t have a space between the words “if” and “(false)”. Which should it be or does it matter?
The SCT (Submission Correctness Test) might not be written to accept the space, but JS doesn’t care. White space is largely ignored by the parsing engine.
Choose a style, after reviewing a couple of style guides, then stick with it so your work is easy to read as a body of code.
Curious about how the computer reads if/else statements, would appreciate any insight on the matter.
I know myself from just writing it out as so that you don’t need else between the curly brackets like } else {, and can instead write it like
end of if
}
else {
}
However i’m curious, does the computer read an if statement, and then for the rest of your code look for an else, unless it hits another if? Or does it have to come directly after the if’s closing curly bracket?
We can write an if statement with or without added white space. JS ignores most of it as long as the expected token or value is present as it parses the statement. Write it all on one line, if you like (though your readers might frown on that).
Where we write the LBRACE and RBRACE is a matter for us to decide, but once decided, stick with that presentation throughout. There are different schools of thought, but none is more right than the other given any of them will work, the proviso above notwithstanding.
My own style uses,
if (...) {
// code
} else if (...) {
// code
} else {
// default
}
Being script, nothing is compiled until it is run. JS may not even see the else if or the else clauses during run time if the opening condition is truthy.
Hello there
Why is that if statements in many programming languages including javaScript does not fallow the math logic namely the Discrete Math logical statements? I mean it only runs if the condition is true? but it does not do anything in the result(q) block when the assumption is false? According to logical statements if the condition (p) is false then the whole statement is true irrespective of what the result will be.
if p => q
if p then q
The answer is simple: Control flow. Discrete Math is abstract. An if statement is imperative.
Abstracts tend to hide a lot of details the mechanics of which are mathematical principles, axioms, theories, etc. which include set theory, analytical geometry, linear algebra, calculus and limits, and so on.
Imperative statements require actions to follow conditional expressions. Control flow must be directed down one path or another to find and execute the next statement. The computer, or language does not possess the power of reasoning. Our programs, at the hand of the programmer must follow prescribed paths from statement to statement without the aid of a human (during runtime).