JavaScript consists of JavaScript statements that are placed in <script>... </script>, HTML tags in a web page.
You can include your JavaScript anywhere on the page by placing <script>, but the best way is to put it inside the <head> tag. </p> The <p><script> tag warns browser programs to begin interpreting all text between these tags as a script. Therefore, the syntax of JavaScript is as simple as: </p> <p></p> <div class="codetitle"> <span><a style="CURSOR: pointer" data="85471" class="copybut" id="copybut85471" onclick="doCopy('code85471')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code85471"><script ...><br> JavaScript code<br> </script>
script tag has two important attributes:
So, your JavaScript snippet should look like this:
<script language="javascript" type="text/javascript"> JavaScript code </script>
First JavaScript script:
Let's write an example to print "Hello World".
<html> <body> <script language="javascript" type="text/javascript"> <!-- document.write("Hello World!") //--> </script> </body> </html>
Javascript code optional HTML comments. Here is the code for browsers that don't support JavaScript. End with "//->" comment. "//" represents a comment in Javascript, so we added it to prevent the browser from reading the end of the HTML comment as a piece of JavaScript code.
Next, we call a function document.write which writes a string to the HTML document. This function can be used to write text, HTML, or both. So, the above code will display the following results:
Hello World!
Spaces and line breaks:
JavaScript ignores spaces, tabs and newlines appearing in JavaScript programs.
Because this way you can freely format and indent your program in a neat, consistent way that makes the code easy to read and understand, you can use spaces, tabs, and newlines freely within your program .
Semicolon is optional:
is generally followed by a semicolon in JavaScript for simple statements, just as they are in C, C# and Java. JavaScript, however, can ignore this semicolon if each statement is placed on a separate line. For example, the following code can be written without using the semicolon
<script language="javascript" type="text/javascript"> <!-- var1 = 10 var2 = 20 //--> </script>
However, when formatting on a line as follows, a semicolon is required:
<script language="javascript" type="text/javascript"> <!-- var1 = 10; var2 = 20; //--> </script>
Note: Using semicolons is a good programming habit.
Case sensitive:
JavaScript is a case-sensitive language. This means that language keywords, variables, function names, and any other identifiers must always be written in consistent one-letter case.
So the identifiers Time, TIme and TIME have different meanings in JavaScript.
Note: Pay attention to variable and function names in JavaScript.
Comments in JavaScript:
JavaScript supports C-style and C-style comments, so:
Example:
<script language="javascript" type="text/javascript"> <!-- // This is a comment. It is similar to comments in C++ /* * This is a multiline comment in JavaScript * It is very similar to comments in C Programming */ //--> </script>