Comments
Single line comment: //
Multi-line comment: /* */
"<!--" can be used as a single-line comment. Since it is similar to the "<!-- -->" multi-line comment of HTML, it is easy to confuse, so this annotation method is not recommended.
variable
In the JavaScript language, the names of variables and other syntax elements are case-sensitive. Variables with the name mood have nothing to do with variables with the name Mood, MOOD, or mOOd, they are not the same variable.
JavaScript syntax does not allow variable names to contain spaces or punctuation marks ("$" exception).
JavaScript variable names are allowed to contain letters, numbers, dollar signs, and underscores (but the first character is not allowed to be a number).
Another way is to use camel format, remove the blank (underscore) in the middle, and each new word afterward starts with capital letters instead: var myMood = "happy";
Data Type
String
Strings must be included in quotes, single or double quotes are OK. You can choose quotes at will, but it is best to choose based on the characters contained in the string. If the string contains double quotes, put 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 ensure that the single quotes between the letters "n" and "t" can be regarded as part of the string. In this case we need to escape this character. In JavaScript, use backslashes to escape characters:
var mood = 'don/'t ask';
Array
Associative array
Traditional array: The subscript of each element is a number. For each element added, the number is increased by 1 at a time.
If only the value of the element is given when filling the array, this array will be a traditional array, and the subscripts of its respective elements will be automatically created and refreshed.
This default behavior can be changed by explicitly giving subscripts for each new element when filling the array. When giving subscripts for new elements, you do not have to be limited to using integer numbers. You can use strings:
The code copy is as follows:
var lemon = Array();
lemon["name"] = "John";
lemon["year"] = 1940;
lemon["living"] = false;
Such an array is called an associative array. Since strings can be used instead of numeric values, the code is more readable. However, this usage is not a good habit and is not recommended for everyone to use. Essentially, when creating an associative array, you create properties of the Array object. In JavaScript, all variables are actually objects of some type. For example, a Boolean value is an object of type Boolean. In the above example, you actually added three attributes to the lemon array name, year and living. Ideally, you should not modify the properties of an Array object, but use a common object.
Object
The code copy is as follows:
var lemon = Object();
lemon.name = "John";
lemon.year = 1940;
lemon.living = false;
The lemon object can also be written as follows:
The code copy is as follows:
var lemon = {name:"John", year:1940, living:false};
Comparison operator
The equality operator == does not mean strict equality, which is easy to be confused. For example, what will result in comparing false to an empty string?
The code copy is as follows:
var a = false;
var b = "";
if(a == b){
alert("a equals b");
}
The evaluation result of this conditional statement is true, why? Because the equality operator == believes that the empty string has the same meaning as false. To make a strict comparison, another equal sign (===). This congruent operator performs strict comparisons, not only comparing values, but also comparing the types of variables.
Of course, the same is true for unequal operators!=. If you want to be strict and unequal, you must use !==.