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

Basic syntax of MySQL index

An index is a sorted data structure! The fields t...

Common parameters of IE web page pop-up windows can be set by yourself

The pop-up has nothing to do with whether your cur...

JavaScript to implement drop-down list selection box

This article example shares the specific code of ...

Nodejs error handling process record

This article takes the connection error ECONNREFU...

CSS style does not work (the most complete solution summary in history)

When we write pages, we sometimes find that the C...

Some tips on deep optimization to improve website access speed

<br />The website access speed can directly ...

Linux kernel device driver virtual file system notes

/******************** * Virtual File System VFS *...

CocosCreator classic entry project flappybird

Table of contents Development Environment Game en...

How to connect to a remote server and transfer files via a jump server in Linux

Recently, I encountered many problems when deploy...

3 ways to correctly modify the maximum number of connections in MySQL

We all know that after the MySQL database is inst...

Priority analysis of and or queries in MySQL

This may be an issue that is easily overlooked. F...

Window.name solves the problem of cross-domain data transmission

<br />Original text: http://research.microso...

Detailed explanation of CSS weight value (cascading) examples

•There are many selectors in CSS. What will happe...