SoFunction
Updated on 2025-03-03

A few key statements of ECMAScript in-depth understanding

Write in front

In this chapter, we will talk about several key statements such as switch, for-in, label, etc. in ECMAScript to deepen our understanding and understanding of them. Let’s start with the most common ones.

While and for

While and for are very common statements, whether in JavaScript, C, or other programming languages. Moreover, in programming, for is more commonly used, which is more flexible and simpler to use, so some people may have such a misunderstanding:

for is more powerful than while and can do some things while not.

In fact, if we think about the syntax application of while and for statements, we will find:

The for loop that cannot be done by while loops cannot be done either.

This is because the for loop just brings together the code related to the while loop. In fact, sometimes it is easier to use a while loop than to use a for loop. This is also because each performs its own duties and has its pros and cons.

Another point about loops, let's first look at a piece of code:

for(i=0;i<5;i++){
  (i);
}
(i);

Print i outside the loop, the printout is 5.

As you can see, variables defined inside the loop can also be accessed externally. In some languages, such as C, braces define block-level scope, but there is no concept of block-level scope in ECMAScript, so variables defined inside the loop can also be accessed externally.

Switch statement

In other programming languages, such as C, switch statements can only use numbers, while in ECMAScript, switch statements can use any data type, such as strings and objects.

Here is a point to be explained: the congruent operator used when comparing switch statements, that is, ===, so '10' and 10 are not equal, because type conversion does not occur when comparing congruent comparisons.

for-in statement

The for-in statement is an accurate iterative statement that can be used to iterate over the properties of an object, and of course it can also iterate over the properties of an array. The following are examples:

for-in traversal objects

•window

First iterate over a special object window:

for(var i in window){
  (i);
}

A very long list of attributes will be printed, and you can check it out by yourself. I will not list them one by one here.

•Custom objects

Iterate through custom objects

var o={prop1:'value1', prop2:'value2', prop3:'value3'};
for (var i in o){
  (i);
}

Print out prop1 prop2 prop3.

•Array

Iterate through the array

var array1=[1,2,3,4];
for(var i in array){
  (i);
}

Printout 1 2 3 4.

with statement

The with statement can be used to limit scopes, that is, the scope of the code can be set to a specific object. as follows:

var hostname=;
var url=;

These two sentences obtain hostname and url respectively. Because they share location (properties under the same object), we can limit the scope to the location, that is, use the with statement to associate the location object. as follows:

with(location){
  
  var hostname=hostname;
  var url=href;
}

It should be noted that using with statements in strict mode will cause syntax errors. At the same time, using with statements in large quantities will lead to performance degradation and will also bring certain difficulties to debugging. Therefore, when developing applications, especially when developing large applications, it is not recommended to use with statements.

label statement

The label statement is used to label the code so that it can be used later. Generally speaking, labeled statements should be used in conjunction with loop statements such as for loops.

Its syntax is:

label: statement

The following is a detailed code to see how labels are used.

1. First give a basic code:

var num=0;
for(var i=0;i<10;i++){
  for(var j=0;j<10;j++){
    if(i==5&&j==5){
      break;
    }
    num++;
  }
}
(num);

Description: break jumps out of the for loop inside, j and the remaining 5 loops are no longer executed, so the print result is 95.

2. Next we change the break to continue:

var num=0;
for(var i=0;i<10;i++){
  for(var j=0;j<10;j++){
    if(i==5&&j==5){
      break;
    }
    num++;
  }
}
(num);

Note: Continue jumped out of this loop, which means jumped out of this loop where the internal for loop was jumped, so the print result was 99.

3. Next, we add a label named outer and look at the printing results separately:

var num=0;
outer:
for(var i=0;i<10;i++){
  for(var j=0;j<10;j++){
    if(i==5&&j==5){
      break outer;
    }
    num++;
  }
}
(num);

Note: After adding the tag, use break to jump to the tag outer, which means that the program jumps out of the outer loop, that is, the program stops execution when the program executes i=5 and j=5, so the printing result is 55.

4. Let's change to continue and see:

var num=0;
outer:
for(var i=0;i<10;i++){
  for(var j=0;j<10;j++){
    if(i==5&&j==5){
      continue outer;
    }
    num++;
  }
}
(num);

Note: This time I use continue, so when the program executes to i=5 and j=5, it does not jump out of the outer loop, but only jumps out of the inner loop, that is, the remaining 5 times will not be executed, so the print result is 95.

Putting these together seems a little confused, and it will be much better to understand more.

summary

We learn something not for learning, but for the purpose of using. To put it bluntly, we learn it for work. Therefore, simply understanding the above things is not the most important thing, not for the sake of remembering, but for the sake of using. Being able to apply it proficiently in practice is what we hope. At the same time, taking notes is a good habit. A good memory is not as good as a bad pen. If you are doing it, I hope you can stick to it.

The above key sentences for in-depth understanding of ECMAScript are all the content I share with you. I hope you can give you a reference and I hope you can support me more.