Race Day JavaScript Project - The "bang" operator question

Hi guys. I have a question about JS and specifically the “bang” operator in the “Race Day” project. My code looks like follows:

let raceNumber = Math.floor(Math.random() * 1000);
const earlyRegistration = true;
const runnerAge = 19;
if (runnerAge > 18 && earlyRegistration) {
  raceNumber += 1000
};
if (runnerAge > 18 && earlyRegistration) {
  console.log(`You are registered to start your race at 9.30 am with the number ${raceNumber}. Good luck!`);
} else if (runnerAge > 18 && !earlyRegistration) {
  console.log(`You are registered to start your race at 11.00 am with the number ${raceNumber}. Good luck!`);
} else if (runnerAge < 18) {
  console.log(`You're set to run at 12.30pm. You're number is ${raceNumber}. Good luck!`);
} else if (runnerAge === 18) {
  console.log('Wow. You\'re exactly 18 years old! Please see registration desk to register!');
  }

Now, the code works exactly as it should, BUT…
This following Else If statement uses the bang to - in my understanding - change the Boolean “earlyRegistration = true” to basically false.

else if (runnerAge > 18 && !earlyRegistration) 

Now, when I tested my code, I was expecting that if I change const earlyRegistration to false, this boolean would flip once more, essentially making the code behave as if the person did in fact arrive early, that’s why in the initial version of my code I stated

else if (runnerAge > 18 && earlyRegistration === false) {
  console.log(`You are registered to start your race at 11.00 am with the number ${raceNumber}. Good luck!`);

Still I find that the “bang” still makes this code run exactly as it should and I don’t understand why.
Could anyone give an explanation please?
Thank you :smile:

Hi there,

This can be read like so: "Else if the runnerAge is greater than 18 and earlyRegistration is not true then…

The ! is not changing the boolean, but assessing if the statement is opposite of true.

Does that make sense?

@kirativewd answered your question really well.
But i have a question why didn’t you end your code with a plain else boolean?
im a noob too

Thanks for your answer. I kind of get it, but I am wandering why earlyRegistration would be true, even if I assigned const earlyRegistration = false? I’ve done so in that exercise, and it still worked as it should have, when in my mind, I expected it to work opposite if that makes sense?
If earlyRegistration = false, shouldn’t !earlyRegistration pretty much look for a true value?

I ended the code with an if else because I’m also a noob :joy: you’re completely right, it should have been ended with an else statement.

I understand you confusion completely, friend. I am still in no way an expert, but find it helpful to come back and help out others who are struggling with the same exact things I did (and still do).

Let us say that earlyRegistration is indeed false. We will have this:

const earlyRegistration = false;
const runnerAge = 19;

.
.
.

if (runnerAge > 18 && earlyRegistration) {
  console.log(`You are registered to start your race at 9.30 am with the number ${raceNumber}. Good luck!`);
} else if (runnerAge > 18 && !earlyRegistration) {
  console.log(`You are registered to start your race at 11.00 am with the number ${raceNumber}. Good luck!`);
} ...

Let us look at what the code is doing first:

if (runnerAge > 18 && earlyRegistration)
  1. Is the runner older than 18?
    -The runner is 19, so that is true.
  2. Did the runner register early?
    -earlyRegistration is false, so the runner did not register early

Because this argument resolves to (true && false), the code moves on to the next else if statement.

else if (runnerAge > 18 && !earlyRegistration)
  1. Is the runner older than 18?
    -The runner is 19, so that is true.
  2. Did the runner register early?
    -earlyRegistration is false, so the runner did not register early, which makes !earlyRegistration true (because false is the opposite of true)

Because this argument resolves to (true && true), the statement is printed

This responds the same way because that argument is the same as above.

  1. Is the runner older than 18?
    -The runner is 19, so that is true.
  2. Did the runner register early?
    -earlyRegistration is false, and false === false, making it a true argument not meaning that earlyRegistration is true

Because this argument resolves to (true && true), the statement is printed. Does that explain it any better for you?

1 Like

ok got it :smile: Goodluck with coding bruv
we will meet again lol

Wow, thanks for taking your time to explain this like an absolute pro. This finally makes perfect sense. Thanks very much for your help!

1 Like