Home > Web Front-end > JS Tutorial > Analysis of label statement usage in JavaScript_javascript skills

Analysis of label statement usage in JavaScript_javascript skills

WBOY
Release: 2016-05-16 16:15:03
Original
1171 people have browsed it

This article analyzes the usage of tag statements in JavaScript with examples. Share it with everyone for your reference. The specific analysis is as follows:

I was watching w3school recently, and then I saw the js part,

<!DOCTYPE html>
<html>
<body>
<script>
cars=["BMW","Volvo","Saab","Ford"];
list:
{
document.write(cars[0] + "<br>"); 
document.write(cars[1] + "<br>"); 
document.write(cars[2] + "<br>"); 
break list;
document.write(cars[3] + "<br>"); 
document.write(cars[4] + "<br>"); 
document.write(cars[5] + "<br>"); 
}
</script>
</body>
</html> 
Copy after login

Seeing that list: I feel a little weird, and then it says

JavaScript Tag

As you saw in the chapter on switch statements, JavaScript statements can be tagged.

To mark a JavaScript statement, precede it with a colon:

label:
statements

Copy after login

The break and continue statements are simply statements that break out of a block of code.

Syntax:

break labelname; 

continue labelname;
Copy after login

continue statements (with or without label references) can only be used within loops.
The break statement (without label reference) can only be used in a loop or switch.
The break statement can be used to break out of any block of JavaScript code, referenced by a tag:

I didn’t pay attention to it at first, but then Baidu marked the javascript statement and read a blog that said this. Let’s learn from it here:

For example:

parser:
 while(token != null) {
 //Code omitted here
}
Copy after login

By labeling a statement, you can give the statement a name, so that you can use this name to refer to it anywhere in the program. You can label any statement,

But the marked statements are usually those loop statements, namely while, do/while, for and for/in statements. Usually the loops are named, and the break statement and continue statement can be used

Exit the loop or an iteration of the loop.

For example:

<script type="text/javascript"> 
  outerloop: 
   for (var i = 0; i < 10; i++) 
   { 
     innerloop:  
     for (var j = 0; j < 10; j++) 
      { 
        if (j > 3) 
        { 
          break; 
        } 
        if (i == 2) 
        { 
          break innerloop; 
        } 
        if (i == 4) 
        { 
          break outerloop; 
        } 
        document.write("i=" + i + " j=" + j + "<br>"); 
      } 
   } 
</script>
Copy after login

After seeing this example, I understood list: clearly. I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template