JavaScript DOM Programming Art The JavaScript grammar _javascript skills of reading notes

Source: Internet
Author: User

Comments
Single-line Comment://

Multi-line Comment:/*/

"<!--" can be used as a single-line annotation and is confusing because it is similar to HTML "<!---->" Multi-line comments, so this annotation method is not recommended

Variable
In a JavaScript language, the names of variables and other syntactic elements are case-sensitive. The variable named mood has nothing to do with the variable whose name is mood, mood, or mood, and they are not the same variable.

The JavaScript syntax does not allow a variable name to contain spaces or punctuation ("$" exceptions).

The JavaScript variable name allows you to include letters, numbers, dollar signs, and underscores (but the first character is not allowed to be numbers).

Another way is to use the hump format, remove the middle whitespace (underline), and each new word in the following is used to start with a capital letter: var Mymood = "Happy";

Data type

String
The string must be enclosed in quotation marks, either single or double quotes. You can select quotes at will, but it is best to select them according to the characters contained in the string. If the string contains double quotes, enclose the entire string in single quotes, and vice versa:

var mood = "Don ' t ask";

If you want to use single quotes in the above statement, you must guarantee that the single quotation mark between the letter "n" and "T" can be used as part of this string. In this case we need to escape this character. To escape a character in JavaScript using a backslash:

var mood = ' don\ ' t ask ';

Array
Associative arrays
Traditional array: The subscript for each element is a number, and each additional element adds an additional 1 to the number.

If only the value of the element is given when the array is populated, the array will be a traditional array, and the subscript of its individual elements will be automatically created and refreshed.

You can change this default behavior by explicitly giving the subscript to each new element when the array is populated. When you give a subscript to a new element, you do not have to limit the use of integer numbers. You can use a string:

Copy Code code as follows:

var lemon = Array ();
lemon["Name" = "John";
lemon["Year"] = 1940;
lemon["Living"] = false;

Such an array is called an associative array. Because you can use strings instead of numeric values, your code is more readable. However, this usage is not a good habit and is not recommended for everyone to use. Essentially, when you create an associative array, you create the properties of the array object. In JavaScript, all variables are actually some kind of object. For example, a Boolean value is an object of a Boolean type. In the example above, you actually added the name, year, and living three attributes to the lemon array. Ideally, you should not modify the properties of an array object, but instead use a generic object.

Object

Copy Code code as follows:

var lemon = Object ();
Lemon.name = "John";
lemon.year = 1940;
Lemon.living = false;

The Lemon object can also be written as follows:

Copy Code code as follows:

var lemon = {Name: "John", year:1940, living:false};

comparison operator

Equality operator = = does not mean strict equality, it is easy to confuse the person. For example, what would be the result of comparing false to an empty string?

Copy Code code as follows:

var a = false;
var B = "";
if (a = = b) {
Alert ("a equals B");
}

The evaluation result for this conditional statement is true, why? Because equality operator = = The null string is considered to be the same as false. To make a rigorous comparison, use another equal sign (= = =). This congruent operator performs a rigorous comparison, not only comparing values, but also comparing the types of variables.

Of course, the same is true for unequal operator!=. If you want to be more rigorous and unequal, you should use!==.

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.