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

Vue component library ElementUI implements table loading tree data tutorial

ElementUI implements a table tree list loading tu...

HTML+CSS to achieve layered pyramid example

This article mainly introduces the example of imp...

How to express relative paths in Linux

For example, if your current path is /var/log and...

Will the index be used in the MySQL query condition?

When an employer asks you whether an index will b...

Some front-end basics (html, css) encountered in practice

1. The div css mouse hand shape is cursor:pointer;...

How to use VUE and Canvas to implement a Thunder Fighter typing game

Today we are going to implement a Thunder Fighter...

A complete explanation of MySQL high availability architecture: MHA architecture

Table of contents 1. Introduction 2. Composition ...

Configuring MySQL and Squel Pro on Mac

In response to the popularity of nodejs, we have ...

Detailed explanation of real-time backup knowledge points of MySQL database

Preface The need for real-time database backup is...

How to solve the problem that MySQL cannot start because it cannot create PID

Problem Description The MySQL startup error messa...

How to install JDK and Mysql on Ubuntu 18.04 Linux system

Platform deployment 1. Install JDK step1. Downloa...

JavaScript singleton mode to implement custom pop-up box

This article shares the specific code of JavaScri...