Home > Web Front-end > JS Tutorial > JavaScript basic syntax explanation_JavaScript

JavaScript basic syntax explanation_JavaScript

WBOY
Release: 2016-05-16 15:57:04
Original
1070 people have browsed it

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>

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>

Copy after login

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>

Copy after login

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!

Copy after login

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>

Copy after login

However, when formatting on a line as follows, a semicolon is required:

<script language="javascript" type="text/javascript">
<!--
 var1 = 10; var2 = 20;
//-->
</script>

Copy after login

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 at the end of the HTML comment is not recognized, so JavaScript should be written as //-->.

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>

Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template