js tag syntax usage details

js tag syntax usage details

Preface:

In daily development, we often use recursion, break , continue , return and other statements to change the location of program execution. In fact, JavaScript also provides label statements to mark specified code blocks, making it easier to jump to specified locations. This article records the use of label statements.

1. Introduction to label statements

Label statements are used to label statements. Labels can have the same name as variables. They are independent syntax elements (neither variables nor types). They are used to identify " labeled statement ", which are equivalent to locators and are used to jump to any position in the program. The syntax is as follows:

label: statement

For example:

    hello:console.log("hello")


Label statements can change the execution flow of a program, similar to break , continue , and return . break and continue can be used with labels.

2. Label statement usage

(1) Use label statements and break statements together to jump out of a specific loop

    let num = 0;
    mylabel:
    for (let i = 0; i < 10; i++) {
      for (let j = 0; j < 10; j++) {
        if (i == 5 && j == 5) {
          break mylabel;
        }
        num++;
      }
    }
    console.log(num); // 55


In this example, outermost label identifies the first for statement. Normally, each loop executes 10 times, which means that the num++ statement will be executed 100 times, and the result of console.log should be 100 when the loop ends. However, the break statement takes a variable, which is the label to exit to. Adding a label causes break to exit not only the inner loop (which uses the variable j), but also the outer loop (which uses the variable i). When i and j are both equal to 5, the loop stops executing and the value of num is 55.

(2) Using label statements with continue

    let num = 0;
    mylabel:
    for (let i = 0; i < 10; i++) {
      for (let j = 0; j < 10; j++) {
        if (i == 5 && j == 5) {
          continue mylabel;
        }
        num++;
      }
    }
    console.log(num); // 95


The continue statement forces the loop to continue executing, but instead of continuing the inner loop, it continues the outer loop. When i and j are both equal to 5, continue will be executed, jumping to the outer loop to continue execution, resulting in the inner loop being executed 5 times less, and the result is num equal to 95.

Summarize:

Combining label statements with break and continue can implement complex logic, but it is also prone to errors. Be careful to use descriptive text for your tags and not to nest them too deeply.

This is the end of this article about the details of js tag syntax usage. For more relevant js tag syntax usage content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • An example of elegant writing of judgment in JavaScript
  • 56 practical JavaScript tool functions to help you improve development efficiency
  • JavaScript implements draggable modal box
  • JavaScript to implement drop-down list selection box
  • JavaScript to achieve simple provincial and municipal linkage

<<:  Implementation of MySQL scheduled backup script under Windows

>>:  How to add vim implementation code examples in power shell

Recommend

Two methods to implement MySQL group counting and range aggregation

The first one: normal operation SELECT SUM(ddd) A...

Share 5 helpful CSS selectors to enrich your CSS experience

With a lot of CSS experience as a web designer, we...

Baota Linux panel command list

Table of contents Install Pagoda Management Pagod...

Example of using docker compose to build a consul cluster environment

Basic concepts of consul Server mode and client m...

Solve the problem that vue project cannot carry cookies when started locally

Solve the problem that the vue project can be pac...

Analysis of the use of the MySQL database show processlist command

In actual project development, if we have a lot o...

Teach you MySQL query optimization analysis tutorial step by step

Preface MySQL is a relational database with stron...

Vue implements multi-column layout drag

This article shares the specific code of Vue to i...

How to explain TypeScript generics in a simple way

Table of contents Overview What are Generics Buil...

Complete step-by-step record of MySQL 8.0.26 installation and uninstallation

Table of contents Preface 1. Installation 1. Down...

Pure JavaScript to implement the number guessing game

Develop a number guessing game that randomly sele...

Complete steps for Docker to pull images

1. Docker pull pulls the image When using $ docke...