JavaScript common statements loop, judgment, string to number

JavaScript common statements loop, judgment, string to number

1. switch

Select one of multiple code blocks to be executed

switch(e) {
     case 1:
        //e is 1, execute break here;
     case 2:
        //e is 2, execute break here;
     default:
        // None of them meet the requirements to execute here} 

Notice:

  • It is not necessary to use break to interrupt the last case in switch code block. The code block ends naturally here.
  • Switch case uses strict comparison (===), the value must be of the same type to be matched.
  • If JavaScript encounters the break keyword, it will jump out of the switch code block, so the system will not continue to execute, which can improve performance.

2. While Loop

If the following condition is not met, the loop will continue until the parameter "i" is not less than 10

while (i < 10) {
    text += "The number is" + i;
    i++;
}

3. Do/While Loop

This is similar to the above, but it will be executed at least once, because it is executed first and then judged.

do {
    text += "The number is " + i;
    i++;
 }
while (i < 10);

  • for - loop executes a block of code a specified number of times.
  • for/in - loop through the properties of an object
  • while - loops through a specified block of code while a specified condition is true
  • do/while - Also loops through a specified block of code while a specified condition is true, but the loop executes the block of code once before evaluating the condition

3. Convert string to number

Many students will encounter the problem of converting strings to numbers, but they can't remember the words to convert, or they are too lazy to write them. In this case, just add a + sign in front of the variable.

Like this:

let a = "12" // This is a string console.log(+a) // Now it is converted to the number 12

This concludes this article about common JavaScript statements for loops, judgments, and string to number conversion. For more related content on common JavaScript statements, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of Javascript basics loop
  • How to use async and await correctly in JS loops
  • Four data type judgment methods in JS
  • About the difference between js typeof and instanceof in judging data types and their development and use
  • An example of elegant writing of judgment in JavaScript
  • How to convert a string into a number in JavaScript
  • parseInt parseFloat js string conversion number

<<: 

>>:  Solution for Baidu site search not supporting https (tested)

Recommend

Detailed tutorial on installing CentOS, JDK and Hadoop on VirtualBox

Table of contents 1. Prerequisites 1.1 Supported ...

MySQL msi installation tutorial under windows10 with pictures and text

1. Download 1. Click the latest download from the...

MySQL installation diagram summary

MySQL 5.5 installation and configuration method g...

CSS World--Code Practice: Image Alt Information Presentation

Using the <img> element with the default sr...

A very detailed explanation of the Linux DHCP service

Table of contents 1. DHCP Service (Dynamic Host C...

Detailed explanation of JavaScript state container Redux

Table of contents 1. Why Redux 2. Redux Data flow...

How to update Ubuntu 20.04 LTS on Windows 10

April 23, 2020, Today, Ubuntu 20.04 on Windows al...

Vue implements a small countdown function

Countdown function needs to be implemented in man...

Essential Handbook for Web Design 216 Web Safe Colors

The color presentation on a web page will be affec...

Vue implements a complete process record of a single file component

Table of contents Preface Single file components ...

Example code comparing different syntax formats of vue3

The default template method is similar to vue2, u...

Two methods to stretch the background image of a web page

There are two solutions: One is CSS, using backgro...

Docker installation and configuration steps for MySQL

Table of contents Preface environment Install Cre...

Teach you how to use vscode to build a react-native development environment

question The code has no prompt: Many non-front-e...