Home > Article > Web Front-end > JavaScript basic syntax explanation_JavaScript
JavaScript consists of JavaScript statements that are placed in 3f1c4e4b6b16bbbd69b2ee476dc4f83a... 2cacc6d41bbb37262a98f745aa00fbf0, HTML tags in a web page.
You can include your JavaScript anywhere on the page by placing 3f1c4e4b6b16bbbd69b2ee476dc4f83a, but the best way is to put it inside the 93f0f5c25f18dab9d176bd4f6de5d30e tag.
The3f1c4e4b6b16bbbd69b2ee476dc4f83a tag warns browser programs to begin interpreting all text between these tags as a script. Therefore, the syntax of JavaScript is as simple as:
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: