create an if statement. Provide the if statement a condition of sale . Inside the code block of the if statement, console.log() the string 'Time to buy!' .
if (true) {
console.log(‘Time to buy!’);
}
// Prints “Time to buy!”
if (sale = true) {
console.log(‘Time to buy!’);
}
// Prints “Time to buy!”
When we compare a value to a boolean literal, true and get a true result, that means the value is itself a boolean literal. There is no need to ever compare to true. Just test the truthiness.
Everything on the right of, => can be coerced to the primitive on the left.
A comparison which uses equality and inequality operators always evaluates to a booleean primitive. Now we mentioned earlier that when a value is already a boolean primitive we can test it with if and while directly.
if (sale)
is the same as,
if (true)
but for one thing: sale is a state whereas true is a definite literal. The former can change, while the latter is immutable.
I do not believe you missed any step. I think you may have just overthought the exercise.
Originally in the exercise, they ask you to create a variable for sale and set it’s value to true.
let sale = true;
Now the variable sale is equaled to true. So if you where to type console.log(sale); this would print true.
To answer your question "what condition is necessary to show that ‘sale’ is ‘true’?
Simply put, the value that you set to the variable sale creates the condition, which in this case equals true
Your variable can have many values or conditions based on what you decide it should be. Example:
let sale = true; // this sets the condition to true.
let sale = false; // this sets the condition to false.
let sale = 10; //this sets the condition to the number 10
let sale = ‘Hello World’; // this sets the condition to a string ‘Hello World’
Now you can check to see if sale is equaled to true by writing an if statement. Example:
if (sale === 10){
To check if a value is truthy (it evaluates to true) we can write it like this:
if (value) {
// Code will get executed if value is truthy
}
Falsy values
An empty string ("", '')
The boolean false
null
undefined
The number 0
NaN
Truthy values => Everything that is not falsy (1, "Hello", true, etc.)
Sometimes, we want to check if a value is falsy (evaluates to false). To do this, we can use the NOT operator:
if (!value) {
// Code will get executed if value is falsy
}
Finally, we can check if a value is equal to a specific value. E.g. if a variable is equal to the numeric value 2
if (value === 2) {
// Code will get executed if value is equal to the numeric value 2
}
=== : The strict equality comparison operator will check if the value on the left-hand side is equal to the value on the right-hand side and if the value on the left-hand side is the same data type as the value on the right-hand side
== : The abstract equality comparison operator will only check if the value on the left-hand side is equal to the value on the right-hand side. It will not check for equal data types meaning "2" == 2 evaluates to true
Porque teve que colocar venda como falso? numa condição verdade nao é sempre primeiro?(fazendo uma comparação ao excel) ou no JS nao funciona assim? na verdade so quero entender de verdade o porque, li a faq mas nao ficou muito claro talvez por conta da traducao q uso automatico mas enfim devo entender nos proximos exe.
No exercício eles pedem para você colocar sale = false; justamente pra você ver que o if não vai imprimir/rodar, já que a variável sale, agora é falsa de acordo com a nossa declaração;
Ola Tassia! obrigada por responder. sim, depois que fiz a pergunta voltei ao exercício reli algumas coisas forum, faq fiz alguns teste faço ainda algumas confusões mas tá fluindo rs. vlw
I gotta be honest here… so far, I’ve loved Codecademy. This lesson is just pure garbage though. Their whole process of force feeding and handholding without alternative examples as to why it works the way it does is pretty darn bad. I get what they think they’re doing… but it’s unproductive for large chunks of time for someone who is learning. Pretty glad I’m just sticking to the free stuff.
You need to know that = (a single equals sign) is an assignment operator and literally assigns a value to a variable.
To check if a conditions is true for now use == (double equals).
if you had typed
if (sale == true) {
}
that kinda would have been logically correct. But doing if(sale) {…} is a SUPER common shorthand that will result in a valid output. And this will always check for the condition to evaluate to TRUE.
I hope this helps.
When using an if statement, the statement is being evaluated if it’s true. And because we set the sale variable (let sale= true), the statement will be evaluated as true. Once our if statement is evaluated as true, the code block containing the console.log will be executed.
If we set the sale variable to false (let sale = false), the if condition is evaluated to false and therefore the code block will not be executed. Hope this helps!