Home  >  Article  >  Web Front-end  >  JavaScript basic syntax explanation_JavaScript

JavaScript basic syntax explanation_JavaScript

WBOY
WBOYOriginal
2016-05-16 15:57:041046browse

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.

The

3f1c4e4b6b16bbbd69b2ee476dc4f83a tag warns browser programs to begin interpreting all text between these tags as a script. Therefore, the syntax of JavaScript is as simple as:

Copy code The code is as follows:
aaf54dea224cb0154e3045720b5063c5
JavaScript code
2cacc6d41bbb37262a98f745aa00fbf0
The

script tag has two important attributes:

  1. Language: The script language specified by this attribute. Typically, its value is JavaScript. Although recent versions of HTML (and XHTML, its successor) no longer use this attribute.
  2. type: This attribute is now recommended to indicate the scripting language used and its value should be set to "text/javascript".

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:

  • All text between // and the end of the line will be treated as a comment and will be ignored by JavaScript.
  • Any text between /* and */ characters is considered a comment. This may span multiple lines.
  • JavaScript also recognizes the opening order of HTML comments
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn