How to Use Semicolon in JavaScript while putting two independent pieces of code on one line ?

How to Use Semicolon in JavaScript while putting two independent pieces of code on one line ?

  1. In JavaScript We are using Semicolon to Separate Different Pieces of Code.
  2. If we forgot to Put Semicolon at the end of Line then it is OK.

i.e -

<script language="javascript" type="text/javascript">
var fname = "Pritesh"
var lname = "Taral"
alert(fname + "" + lname)
</script>
  1. End of Line Semicolon is Optional in JavaScript. (as seen in above example)
  2. If you are going to write two independent executable codes on Single Line then You must Write Semicolon.
  3. Semicolon will tell browser that - “End of Statement” has been encountered.

Example Of Using Semicolon :

<script language="javascript" type="text/javascript">
var fname = "Pritesh" ; var lname = "Taral" ;
alert(fname + " " + lname)
</script>

Explanation :

Above Program Combines two Variable Declaration Statements on Single Line that’s why we need to write semicolon in between to indicate that end of statement has been encountered.