# Why does this programme return false?

``````let earlyReg = false;

let runnerAge = 20;

const earlyAndOld = earlyReg = true && runnerAge > 18;

const old = earlyReg = false && runnerAge > 18;

const young = runnerAge < 18;

console.log(old)
``````

What is your expected outcome from that statement?

Hint:

`false && 'anything'` will always yield, `false`.

2 Likes

Well my hope was that since the first variable was false, the statement that evaluated if that statement was false would come out true, since i told it to see if something was false…
If that makes sense? If you need a better explanation i can try to offer it?

`False` will short-circuit AND so will always be false. We cannot AND false and expect anything different.

Also, it is pointless to compare to a boolean. Use truthiness, instead.

``````earlyReg && runnerAge > 18
``````
2 Likes

But then how do i make it so that something happens if earlyreg is false?

Describe the problem, or post a link to the exercise so we can read what is instructed.

1 Like

What do you want to happen if earlyReg is false?

1 Like

well basically i just want it to log stuff to the console.

The `=` operator is used for assignment. For comparisons, you should use the equality operator `==`.

``````// You wrote:
const earlyAndOld = earlyReg = true && runnerAge > 18;

// It should be:
const earlyAndOld = (earlyReg == true) && (runnerAge > 18);
``````

However, as mtf pointed out, when comparing with booleans, it is better to use truthiness i.e.

``````// Instead of:
const earlyAndOld = (earlyReg == true) && (runnerAge > 18);
// better to use:
const earlyAndOld = (earlyReg) && (runnerAge > 18);

const old = (earlyReg == false) && (runnerAge > 18);
// better to use:
const old = (!earlyReg) && (runnerAge > 18);
``````

I have used parentheses for clarity. If you prefer, you can omit them.

If you use assignment operator instead of comparison, then

``````let flag = false;

if (flag = true) {
console.log("Condition met!");
}

console.log(flag);

// Output:
"Condition met!"
true

// The assignment operator assigned true to the flag variable.
``````

as opposed to comparison operator:

``````let flag = false;

// Using (flag == true) just as an example.
// if (flag) { ... would be better.

if (flag == true) {
console.log("Condition met!");
}

console.log(flag);

// Output:
false
``````
1 Like

i’m a little unclear here? it seems to have become true without being specifically changed?

``````let flag = false;

if (flag = true) {
console.log("Condition met!");
}

console.log(flag);

// Output:
"Condition met!"
true
``````

If you use the assignment operator `=`, then you are specifically changing the value assigned to the variable.
Even though the assignment operator is being used in the condition, the assignment is still made.

``````if (flag = true) { ...
``````

Here is a sketch of how the above will be evaluated:

• First, the expression within the parentheses will be evaluated (so that the decision to execute or skip the if block can be made).

• Since the assignment operator `=` has been used (instead of the comparison operator `==`), so the boolean value `true` will be assigned to the `flag` variable. Whatever value had been previously assigned to `flag` would be over-written.

• After the assignment has been made, the expression `if (flag = true) {` is equivalent to `if (flag) {`. Since `flag` is truthy, so the expression evaluates to `true` and the `if` block is executed.

When making comparisons, don’t use the assignment operator `=`
Instead use the equality comparison operator `==`

1 Like

ah, thank you! i always thought it only assigned things if flag=true was on it’s own.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.