Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

which is the proper syntax for javascript?

var age = prompt("Enter your age");
  if (age < 18){
   alert("Sorry, you are too yound to drive this car. Powering off");
   }
  else if (age > 18){
   alert("Powering On. Enjoy the ride!");
   }
  else if(age == 18){
   alert("Congratulations on your first year of driving. Enjoy the ride!");
   }

or

var age = prompt("What is your age?");

  if (Number(age) < 18) {
    alert("Sorry, you are too yound to drive this car. Powering off");
  } 
  else if (Number(age) > 18) {
    alert("Powering On. Enjoy the ride!");
  } 
  else if (Number(age) === 18) {
    alert("Congratulations on your first year of driving. Enjoy the ride!");
  }

which of the syntax is better? and why?…relating to the Boolean condition of the if/else…if statement

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That is not a question of syntax, the code you provided is good in any cases. However, if you want an accurate result and a clean code, you should transform your prompted result into a number. Keep in mind that Javascript in a dynamic typed language, so if the number as a string is compared to a number, they both will be compared as string.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading