HTML+JS+CSS Basic-2.javascript
2. javascript
javascript as a scripting language, syntax and Java there are similarities. For example, comments use double slashes (//), and semicolons (;) as the end of expressions…
2.1 Common types
Common types are: String, Number, Boolean, Array, Object.
2.1.1 String
Strings can be wrapped in single or double quotes, and strings can be spliced using the plus sign (+).
It is created as follows:
1 | var str1 = "str1"; |
Common APIs:
1 | // Determines if a string starts with str, returns true or false. |
Other than that, it is basically the same as java usage.
2.1.2 Number
Can represent either integers or decimals; too large or too small a value can be expressed in scientific notation.
1 | var y=123e5; // 12300000 |
The Number type has a more specific object NAN, which represents non-numeric values.
1 | typeof NaN; // "number" |
A NaN object is not equal to any object, including itself:
1 | NaN == NaN //false |
You can use the js built-in function isNaN() to determine this:
1 | var nan = NaN; |
When a string can not be converted to a number, return NaN means parsing failure; undefined and NaN arithmetic operations will return NaN; invalid parameters of mathematical functions, such as the square root of a negative number, the logarithm of a negative number, will return NaN; there is an infinity in js, infinity multiplied by 0 will return NaN:
1 | var result = 1/0; //Infinity |
2.1.3 Boolean
has only two values: true and false; note that it is case sensitive.
2.1.4 Arrays
Arrays in js do not require the same type of elements, but try to keep the element types consistent for easy maintenance.
The way to create an array is as follows:
1 | var test_arr = ["a", 1, null]; |
Common APIs:
1 | //1.Taking and assigning values using subscripts |
Other than that, it is basically the same as java usage.
2.1.5 Types of objects
1 | var car = { |
2.1.6 typeof operator
Returns the approximate type of an object as a string.
1 | typeof new String("str1"); // "object" |
2.2 Null Objects
There are two empty objects in js, null and undefined, with the following differences:
(1) null is of type Object; undefined is Undefined
1 | typeof null; // "object" |
(2) undefined indicates that an expected value or parameter is missing; a variable declared using var is undefined when it is not assigned a value, when a function is passed as a parameter it is undefined, and when an undefined function returns a value - it returns undefined;
(3) null is often used to explicitly represent an empty or invalid object, array, or variable; to empty a variable or release a reference to an object, set it to null.
2.3 Variables and scopes
Variables are declared using var in ES5 and earlier versions of js, and let and const were introduced in ES6; common browsers are compatible with these two keywords and can use them directly.
2.3.1 The var keyword
- Functional level scoping: the scope of a variable is restricted to inside the function in which it is declared, and accessing it from outside the function will result in an error.
- Variable promotion: variable declarations are promoted to the top of the scope.
- Repeatable declarations: subsequent declarations overwrite previous ones.
2.3.2 The let keyword
- Block level scoping: the variable is scoped to the block it is in, i.e. accessing it from outside the block will result in an error.
- No repeated declarations: within the same scope, the same variable cannot be declared repeatedly using let.
- No variable promotion: the variable declared by let can only be used after the declaration statement.
2.3.3 The const keyword
- Constant: cannot be modified after being assigned a value.
- Block level scope: same as let, block level scope.
- Not to be repeated: within the same scope, using const to declare the same variable several times will report an error.
The characteristics and limitations of let and const can advance the possible introduction of hot misoperation in var to the development stage, thus avoiding bugs; therefore, it is recommended to use let and const instead of var in the development process.
2.4 Common String and JSON Operations
1 | //1.JSON.stringify() Converting objects to JSON strings |
2.5 if and switch-case
1 | if (conditional expression 1) { |
if-else syntax and Java is basically the same, need to pay attention to the conditional expression for the variable, the zero value to false, non-zero value to true.
1 | let scoreLevel = "A"; |
The syntax of switch-case is the same as that of Java, and the usage of passthrough and break is also the same.
2.6 Try-catch exception handling
1 | try { |
The exception object has name and message information.
2.7 for loops and while loops
1 | for (var i = 1; i <= 5; i++) { |
for and while usage with Java, continue and break usage is also the same.
2.8 With or Without
with(&&), or(||), not(!) is exactly the same as Java
2.9 Equality
== determines whether only values are equal; === determines whether values and types are equal.
1 | var str1 = "1"; |