ECMAScript automatically converts the result of the condition expression into a Boolean by calling the Boolean()
casting function on it.
It’s considered best coding practice to always use block statements, even if only one line of code is to be executed. Doing so can avoid confusion about what should be executed for each condition.
The for-in statement is a strict iterative statement. It is used to enumerate the non-symbol keyed properties of an object.
It used to enumerate properties’s name rather than value.
Object properties in ECMAScript are unordered, so the order in which property names are returned in a for-in statement cannot necessarily be predicted.
The for-of statement is a strict iterative statement. It is used to loop through elements in an iterable object.
The for-of loop will iterate in the order that the iterable produces values via its next()
method.
Note that the for-of statement will throw an error if the entity that it is attempting to iterate over does not support iteration.
In ES2018, the for-of statement is extended as a for-await-of loop to support async iterables which produce promises.
start: for (let i = 0; i < count; i++) {
console.log(i);
}
In this example, the label start can be referenced later by using the break or continue statement. Labeled statements are typically used with nested loops.
let num = 0;
outermost:
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
if (i == 5 && j == 5) {
continue outermost;
}
num++;
}
}
console.log(num); // 95
Using labeled statements in conjunction with break and continue can be very powerful but can cause debugging problems if overused. Always use descriptive labels and try not to nest more than a few loops.
The with statement sets the scope of the code within a particular object. The syntax is as follows: with (expression) statement
.
The with statement was created as a convenience for times when a single object was being coded to over and over again.
let qs = location.search.substring(1);
let hostName = location.hostname;
let url = location.href;
It equals:
with(location) {
let qs = search.substring(1);
let hostName = hostname;
let url = href;
}
In strict mode, the with statement is not allowed and is considered a syntax error.
It is widely considered a poor practice to use the with statement in production code because of its negative performance impact and the difficulty in debugging code contained in the with statement.
switch ("hello world") {
case "hello" + " world":
console.log("Greeting was found.");
break;
case "goodbye":
console.log("Closing was found.");
break;
default:
console.log("Unexpected message was found.");
}
Best practices dictate that a function either always return a value or never return a value. Writing a function that sometimes returns a value causes confusion, especially during debugging.
eval
or arguments
.eval
or arguments
.Function 有声明提升(declaration hoisting).
if(false){}
包裹着).In non-strict code, function declarations inside blocks behave strangely. Do not use them.
Reference: