JavaScript Learning Series 1 syntax

Source: Internet
Author: User

JavaScript code can be executed only through HTML/XHTML documents. There are two ways to do this.
1. Place the JavaScript code between the <script> labels in the <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
<Script>
<! -- JavaScript Goes Here --> www.2cto.com
</Script>
</Head>
<Body>
</Body>
</Html>
 
2. Another better way is to save JavaScript code as an independent file with the extension. js. A typical practice is to place a <script> label in the <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
<Script src = "file. js"> </script>
</Head>
 
<Body>
</Body>
</Html>
 
But the best practice is to put the <script> tag at the end of the HTML document, before the </body> tag:
 
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
</Head>
<Body>
<! -- Mark-up goes here -->
<Script src = "file. js"> </script>
</Body>
</Html>
 
Annotations in JavaScript
// Single line comment
<! -- Single line comment, no need to close
/*
Multi-line comment
*/
JavaScript is a weak type language. In other words, you do not need to specify the type of a variable. You can store variable values without worrying about its type.
Array initialization:
Var beatles = Array (4); limited length
Var beatles = Array ();
Beatles [0] = "John ";
Var beatles = Array ("A", "B", "C ");
Var beatles = ["A", 234, false];
Associated array: You can specify the subscript for each new element by filling in the array. A string can be used for the next mark.
Var lennon = Array ();
Lennon ["A"] = "John ";
Lennon-[ "B"] = 1209;
This method is not recommended.
Object:
Var lennon = Object ();
Lennon. name = "John ";
Lennon-year = "1940 ";
Var lennon = {name: "John", year: 1940 };
Naming: variables are separated by underscores (_), and functions use the hump naming method.
Comparison operators: = and! In JavaScript! = Is not strictly used. When you compare 3 = "3", the result is true. If you want to strictly compare them, you 'd better use = and! =

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.