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 ?
- In JavaScript We are using Semicolon to Separate Different Pieces of Code.
- 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>
- End of Line Semicolon is Optional in JavaScript. (as seen in above example)
- If you are going to write two independent executable codes on Single Line then You must Write Semicolon.
- 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.