Basic JavaScript syntax

Source: Internet
Author: User
Tags html comment

Basic JavaScript syntax

This article mainly introduces the basic syntax of JavaScript, including the basic knowledge of comments and writing formats. For more information, see

JavaScript consists of the HTML tag of the JavaScript statement placed in the webpage.

You can set

The script tag has two important attributes:

Language: used by the script language specified by this attribute. Generally, its value is JavaScript. Although this attribute is no longer used in recent versions of HTML (and XHTML, its successor.

Type: this attribute is currently recommended to indicate the scripting language used and its value should be set to "text/javascript ".

Therefore, your JavaScript snippet should be like this:

?

1

2

3

<Script language = "javascript" type = "text/javascript">

JavaScript code

</Script>

The first JavaScript script:

Let's write an example to print out "Hello World ".

?

1

2

3

4

5

6

7

8

9

<Html>

<Body>

<Script language = "javascript" type = "text/javascript">

<! --

Document. write ("Hello World! ")

// -->

</Script>

</Body>

</Html>

Javascript code can be annotated in HTML. The code here does not support JavaScript browsers. End with "//->. "//" Indicates a comment in Javascript, So we add a JavaScript code that prevents the browser from reading the end Of the HTML comment.

Next, we call the document. write function to write the string to the HTML document. This function can be used to write text, HTML, or both. Therefore, the above Code will display the following results:

?

1

Hello World!

Space and line feed:

JavaScript ignores spaces. tabs and line breaks appear in JavaScript programs.

In this way, you can freely format and indent programs in a neat and consistent manner, so that the code is easy to read and understand. You can use spaces, tabs, line breaks and free in your program.

Semicolon is optional:

Generally, it is followed by a semicolon in a simple statement in JavaScript, just because they are in C, C ++, and Java. JavaScript, but you can ignore this semicolon if each statement is placed in a separate row. For example, the following code can be written without a semicolon

?

1

2

3

4

5

6

<Script language = "javascript" type = "text/javascript">

<! --

Var1 = 10

Var2 = 20

// -->

</Script>

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

?

1

2

3

4

5

<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 the keywords, variables, function names, and any other identifiers of the language must always be written in the same case with one letter.

Therefore, the identifier Time, TIme, and TIME have different meanings in JavaScript.

Note: Pay attention to the variables and function names in JavaScript.

Annotations in JavaScript:

JavaScript supports C-style and C ++-style annotations. Therefore:

// All text between the end of the line will be treated as a comment and will be ignored by JavaScript.

Any text character between/* and */is considered as a comment. This may span multiple rows.

JavaScript also acknowledges that the opening sequence of HTML comments cannot be identified, so JavaScript should be written as // -->.

Example:

?

1

2

3

4

5

6

7

8

9

10

11

<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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.