Syntax
All JavaScript statement must be put in this two way:
- Place all the JavaScript code in between the <script>…</script> tag or
- Create the .js file and place all the JavaScript code in that file without the <script>…</script> tag.
We can place <script>…</script> anywhere in the html page. But most of developer always put the all JavaScript code in between the <head>…</head> tag.
The <script> tag notify the browser that now it is the JavaScript statement and start the executing that JavaScript statement.
Syntax:
<script> //JavaScript statement </script>
Script tag have 2 attributes:
- Language: This attribute specifies what scripting language you are using. Typically, its value will be javascript.
- Type: This attribute is what is now recommended to indicate the scripting language in use and its value should be set to “text/javascript“.
<script language="javascript" type="text/javascript"> //JavaScript statement </script>
But we can omit this 2 attribute, Because the most modern browser of today can understand that this is the JavaScript code and browser can run it safely. But it is recommended to use these two attribute always with the <script> tag.
Small JavaScript Code
<html> <head> <script language="javascript" type="text/javascript"> document.write("Script tag from the <b>head</b> tag!!!<br/>"); </script> </head> <body> <script language="javascript" type="text/javascript"> document.write("Script tag from the <b>body</b> tag!!!"); </script> </body> </html> //Script tag from the head tag!!! //Script tag from the body tag!!!
Semicolons are Optional
Till this date many developer does not know that semicolons are optional in JavaScript, but we recommended you that always use the semicolons after the every statement of the JavaScript. Because it helpful when we have to minified the JavaScript file.
If you want to omit the semicolons than you have to write one statement per line. It means one line have only one statement.
With out semicolons:
<script language="javascript" type="text/javascript"> var a = 0 var b = 0 </script>
With semicolons:
<script language="javascript" type="text/javascript"> var a = 0; var b = 0; </script>
But when you minified the JS file than without semicolons file will not work.
Minified file without semicolons
<script language="javascript" type="text/javascript"> var a=0var b=0 </script>
Minified file with semicolons
<script language="javascript" type="text/javascript"> var a=0;var b=0; </script>
Comments:
Most of the programming language follow the same style for the comment the code.
Single line Comment:
For single line comment we can use the double slash (//), Syntax:
<script language="javascript" type="text/javascript"> var a = 0; var b = 0; //var c = "This is the comment"; </script>
Multiple line comments:
For multiple line comments we can use the /*…*/ syntax:
<script language="javascript" type="text/javascript"> var a = 0; var b = 0; /*var c = "This is the comment"; var d = "This is also comment";*/ </script>
Happy Coding...
🙂