What condition is necessary to show that `sale` is `true`?

This exercise just threw me.
I was expecting to put an argument in the if statement.
eg. if(sale = true){

      }

not just (if sale){}

Where did I miss this crucial step?

I went elsewhere to find that a varialbe not assigned a value is a primitive boolean ‘0’, while a variable assigned a value is a primitive boolean ‘1’

3 Likes

Hi Ramjet,
Your first argument is right, but I don’t know they did not explain to us the different between (sale) and (sale = true).
If you type only (sale), it should work.

2 Likes

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!”

I can not go to task 3?

Recall that = is the assignment operator and is not used for comparison. Instead we use, === to test identity.

sale [isIdenticalTo] true

However, it is not necessary to compare to a boolean. We can simply test for truthiness

sale = ""
if (sale) {
    //
} else {
    console.log('Not true')
}
9 Likes

everytime i in put if (true) {
} the console log part all turns blue instead of the normal color, what am i doing wrong?

Can someone explain this please?
The user above me asked a question but no response. I have the same issue:

"I was expecting to put an argument in the if statement.
eg. if(sale = true){

      }

not just (if sale){}

Where did I miss this crucial step?

I went elsewhere to find that a varialbe not assigned a value is a primitive boolean ‘0’, while a variable assigned a value is a primitive boolean ‘1’

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.

false => 0, '', "", null, undefined
true  => 1, ' ', " ", NaN, {}, []

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.

9 Likes

Does this mean that if statements default to true?

6 Likes

I think you are right.

If someone can explain if the “if” statement is true by default ?

if (condition) {
  // condition must evaluate to `true` (must be truthy)
  // to be able to enter this branch
}
2 Likes

Ramjet64,

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){

console.log(‘This statement is true’)}

else if(sale === ‘Hello World’){

console.log(‘This statement is true’)}

else{

console.log(‘This statement is false’)};

Hope this helps!

3 Likes

Just as you can say

if (sale === true) {
console.log("Time to buy!");
}

OR

if (true){
console.log(“Time to buy!”);
}

Can you also say something like,

if (false){
 console.log("Time to buy!")
}

There by testing for falsiness rather than truthiness?

The code in the block will not run. IF can only let a truthy value into the code block. To test for falsiness we would use the NOT operator…

if (! sale) {
    // sale in this case is falsy
}

In other words, the only values that will meet the conditional are,

  • undefined
  • null
  • NaN
  • “”
  • 0
  • BigInt 0n
  • false
3 Likes

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

2 Likes

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

1 Like