Am I using semicolons right in js?

Tags:



I have been doing some basic js but I am not sure if I am using semicolons correctly. Here is my code:

//creates a variable that will start the game
var start = confirm("Are you sure want to participate in plonker base alpha?");

//starts and loops the game
if(start){
  //asks for another person's name
  var person1 = prompt("Please name one of your best friends.")
}

//creates a randomizer function
var random = function (subject){
  return subject[Math.floor(subject.length * Math.random())]
}

while(start){
  //creates array 'person'
  var person = ["You are ","Your mum is ","Your dad is ", "The world is ",(person1 + " is ")];
  var personGenerator = random(person);

  //creates an array 'offence'
  var offence = ["an idiot!",
    "a complete pysco!!!",
    "a smelly, worthless peice of junk!",
    "a whale re-incarnated that looks like a squirrel!",
    "a dumb pile of dirt that has the misfortune of seeing itself in the mirror once in a while!",
    "a complete and utter plonker!",
    "a dumbo!",
    "a right dufus!!!",
    "a pile of rabbit dung!",
    "an intelligant, good looking king being... Did I mention - it's opposite day!",
    "a bum-faced rat!!!",
    "a fat, lazy oaf!",
    "a blobfish look-alike!!!!!",
    "a lump of toenail jelly!"
  ];

  var offenceGenerator = random(offence);

  //gives out the offence
  alert(personGenerator + offenceGenerator);
}
{
  alert("What a plonker!")
}

Please correct me in the comments if I am using them wrong.

Thanks, Reece C.

Answer

Modify the following lines and it’d look good to me, semi-colonwise.

var person1 = prompt("Please name one of your best friends.");
return subject[Math.floor(subject.length * Math.random())];
alert("What a plonker!");

The semi-colon is only required in JavaScript when two statements are on the same line, like this:

i=0;j++

Therefore, the semi-colon can be happily omitted when statements are separated by a line break, like this:

i=0
j++

However, ending each statement with a semi-colon may be considered a more disciplined approach (this way all statements will end the same way) and could help you avoid mysterious bugs later.

More information can be found here, here, and here. See also, this SO question.



Source: stackoverflow