JavaScript if statement!

Hi everyone! I’m new to HTML, CSS & JS any support would be much appreciated…I’m basically trying to display if someone is old enough to vote in the UK using the if statement in JavaScript. I can either display on the browser or console. I believe I am missing parts of code.

This is what I have done so far:
Const age = 18;
If (age >= 18) {
Console.log(“you’re eligible to vote in the UK.”);
Else {
(“Sorry you’re not old enough to vote in the UK yet!”);

If true it should display the if sentence if false the else sentence if it makes sense?

On my console it only appears to display the true sentence?

Thank you in advance

hi @cloud3436942542 ,

your statement absolutely make sense. No problem about it. Just make sure everything is coded case sensitively will do.

P.S. Do tag this forum post with #get-help:javascript as well so it’ll be registered in that group of the forum in future/now

Hi estforesta

Thank you for your reply :+1:t2:
I have change the code for case sensitive but on my console it only appears the true Boolean?
Do I need to create a user input? So right now I only had e a JS file and the above code and once I run the program only has one input which is “you are old enough to vote”

Sorry don’t know how to express myself in a better way :grimacing:

You can refer to this link for the method of including the components to make your variable to store dynamic components.

const age = 18; // This line would need to change to the component to read from browser or console line if (age >= 18) { console.log("you're eligible to vote in the UK."); } else { console.log("Sorry you're not old enough to vote in the UK yet!"); }

Should you not understand, feel free to ask.

Perfect thank you for your help :ok_hand:

Hey estforesta, me again Lol.

Still can’t display the false statement on my console/output?

This is what my code looks like so far:

const input = prompt(“Please enter your age:”);
const age = 18;
if (age >= 18) {
console.log(“you are eligible to vote in the uk.”);
else {
console.log(“sorry, you’re not old enough to vote in the uk yet!”);

So i’ve created the user input for age but no matter what age I entered the outcome only displays:
you’re eligible to vote!
Any suggestions on how can I fix this pls?

many thanks

I´ve tried this and it works. Did you really follow the syntax in our lessons? I did - maybe too strictly ;-).
Try to use the DevTools. They can give you great hints. Happy coding!

 const canYouVote = () => {
   let ageOfVoter = Number(prompt("How old are you?"));
   if (ageOfVoter >= 18) {
      console.log("you are eligible to vote in the uk.");
   } else {
      console.log("sorry, you\’re not old enough to vote in the uk yet!");
1 Like

@cloud3436942542 ,

exactly as what @marcoman8979832258 mentioned, the syntax you created is correct, but what about the condition statement that you’re writing at this moment?

There’s two variables, input and age, being created here, but your IF condition is reading/comparing one of the variable and thereafter, produces the result you have wrote.

You’re about there already. Just need to replace ONE thing and you’ll reached you answer :grinning_face_with_smiling_eyes:

Thank you for help :slight_smile:

Great! thank you for your help :slight_smile:

You are welcome :slight_smile: Good luck!