Statement
For statement
- For the initialization expression in the For statement, both the control expression and the post-loop expression are optional and the three expressions are omitted, creating a wireless loop.
- There is no block-level scope in ECMAScript, so variables defined in the Loop Content section can also be accessed externally.
for-in statements
A property used primarily to enumerate objects ( arrays are also one of the objects )
var obj = { A: ' This was a ', B: ' This is B ', C: ' This is C '};var arr = [ ' arr1 ', ' arr2 ', ' Arr3 '];for (v Ar o in obj) { console.log (o + ': ' + obj[o]); } /* Results A:This is a b:this is b c:this are c*/for (var k in arr) { console.log (k + ': ' + arr[k]); } /* Results 0:arr1 1:arr2 2:arr3*/
Laber,break and Continue statements
Label statements can be labeled in code for future use, usually with loop statements such as for statements, with break and continue to enable more than one exit loop operation ( Normal break and continue can only exit one loop )
Here is an example of the code
var num = 0;outermost:for (var i=0; i<10; i++) {for (var j=0; j<10; J + +) { if (i = = 5 && J = = 5) { B Reak outermost; } num++;} } alert (num); 55//-------------------------------------------------------------var num = 0;outermost:for (var i=0; i<10; i++) { For (var j=0; j<10; J + +) { if (i = = 5 && J = = 5) { continue outermost; } num++;} } alert (num); 95//-------------------------------------------------------------var num = 0;outermost:for (var i=0; i<10; i++) { For (var j=0; j<10; J + +) { if (i = = 5 && J = = 5) {break ; } num++;} } alert (num); 95//-------------------------------------------------------------var num = 0;outermost:for (var i=0; i<10; i++) { For (var j=0; j<10; J + +) { if (i = = 5 && J = = 5) { continue; } num++;} } alert (num); 99
Switch statement
The switch statement in JavaScript is flexible and you can use it to handle multi-branch situations. ( note that after each case statement block, a break is added, unless it is intentionally done, the execution of a case statement block continues, knowing that abreak is encountered)
The code is as follows:
var num = 25;switch (true) {case num < 0:alert (' less than 0. '); Break;case num >= 0 && num <= 10:alert (' between 0 and 10. '); Break;case num > && num <= 20:alert (' between and 20. '); Break;default:alert (' More than 20. ');} The result is output more than 20.
Function
Tips
statement return; The return value is undefined;
Understanding Parameters
- The JS function does not mind passing in the number of parameters, also do not care about the data type of the passed in parameters, all pass in parameters can be accessed through the arguments object, the object is similar to an array but not an instance of arrays, such as the first parameter can be accessed through arguments[0], And so on
- The length property of the arguments object tells you how many arguments are passed to the function. Because of this property, we can easily simulate the function overload that JS does not have. Such as:
function Doadd () {if (arguments.length = = 1) {alert (Arguments[0] +),} else if (arguments.length = = 2) {alert (arguments[0 ] + arguments[1]);}} Doadd (//20doadd); (30, 20); 50
- Arguments objects can be used with named parameters, and their values remain in sync
Basic JavaScript concepts (statements and functions)