JavaScript is a lightweight programming language, and JavaScript is a programming code that can be inserted into an HTML page, and JavaScript is inserted into the HTML page and can be performed by all modern browsers.
JavaScript If ... Else Statement
How it works: first set an expression n (usually a variable). The value of the subsequent expression is compared to the value of each case in the structure. If there is a match, the code block associated with the case is executed. Use break to prevent your code from automatically running down a case.
Switch (n) {Case 1: break;case 2: break;default:}
JavaScript for LoopIf you want to run the same code over and over again, and the values are different each time, it is convenient to use loops.
document.write (Cars[0] + "<br>");d Ocument.write (cars[1] + "<br>");d Ocument.write (cars[2] + "<br>") ;d Ocument.write (cars[3] + "<br>");d Ocument.write (cars[4] + "<br>");d Ocument.write (cars[5] + "<br>" );
JavaScript While LoopLoops can execute code all the time, as long as the specified condition is true.
While loopThe while loop executes a code block when the specified condition is true.
Grammarwhile (condition) { code to execute }
JavaScript Break and Continue statementsThe break statement is used to jump out of a loop.
The Continue is used to skip an iteration in the loop.
Break statementWe have seen break statements in earlier chapters of this tutorial. It is used to jump out of a switch () statement.
The break statement can be used to jump out of a loop.
After the break statement jumps out of the loop, the code after the loop resumes (if any):
Instancefor (i=0;i<10;i++) { if (i==3) { break;
} x=x + "The number is" + i + "<br>"; }
JavaScript Variables1. As with algebra, JavaScript variables can be used to hold values (such as x=2) and expressions (such as z=x+y).
Variables can use short names (such as x and y), or they can use better descriptive names (such as age, Sum, totalvolume).
2. Naming requirements for variable names
① variable names can only be composed of alphanumeric underscores.
② cannot start with a number.
③ variable names are case-sensitive, and uppercase and lowercase letters are different variables. (Plain capital letters are generally used to denote constants)
3. Naming conventions for variable names
① conforms to the small hump rule (hump rule) Multiple words consist of variable names, with the exception of the first letter lowercase and the first letter of each word capitalized.
Mynameisli
② or use Hungarian nomenclature, all letters lowercase, and the words are separated by an underscore _.
4. Data types in JS (remember)
The ①undefind is not assigned, and the variable declared with VAR has been used but is not assigned a value.
②nall represents an empty reference
The ③boolean boolean type. True Ture, Flase.
④number numeric type, can be an integer, can be a decimal,
⑤string string type, with "" or "package
⑥object Object Type
JavaScript Data TypesUse the VAR keyword to declare the variable:
var carname;
After the variable declaration, the variable is empty (it has no value).
To assign a value to a variable, use the equals sign:
Carname= "Volvo";
However, you can also assign a value to a variable when you declare it:
var carname= "Volvo";
JavaScript variables can also hold other data types, such as text values (name= "Bill Gates").
In JavaScript, a text like "Bill Gates" is called a string.
There are many types of JavaScript variables, but for now, we only focus on numbers and strings.
When you assign a text value to a variable, enclose the value in double or single quotation marks.
Do not use quotation marks when the value you assign to a variable is numeric. If you enclose a value in quotation marks, the value is treated as text.
JavaScript OperatorsArithmetic operations (single-mesh operators)
+,-, *,/(reserved decimals),%, + + self-increment,--auto minus
+ addition operation, connection string, except add extra, the remaining symbols will first try to turn the left and right side number
[a++/++a similarities and differences], (--the same)
A=1 b= a++ +2 >>>b=3 a=2 First performs the addition with the value of a, and then adds a.
c= ++a +2 >>>5 perform a self-increment first, then add
Assignment operations
= += -= *= /= %=
=: assigned value;
+=:a+=b; equivalent to a=a+b;
Relational operations
= =,! =, >, <, >=, <=
The result of the relational operator operation is the Boolean type Ture/flase.
"To determine whether a number is in a certain range must be used && link.
= = equals, the type is the same direct judgment, different types will try to convert both sides to number and then judge.
! = Not equal to
= = = Strictly equal to the value type must be the same, the type of the result is false, the same type and then the next judgment.
! = = Not strictly equal to
Conditional operator (multi-mesh operation)
A>b?true:false
Have two important symbols? And:
When the part of the question mark before the result is ture execution: The preceding code, flase execution: The following code: The two sides can be numeric, the entire formula can be used to assign a value. Both sides of the colon are code blocks, and code is executed directly.
Multi-mesh operations can be nested in multiple layers
Bitwise operators, logical operators
&&, | |,!
Precedence of Operators
()
! ++ --
* / %
+ -
> < >= <=
== !=
&& and/or simultaneous presence vs. or high
||
= + = *=/= The lowest is a variety of assignments
Basic use of JavaScript summary ①