This article brings you a detailed explanation of the process statements in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Overview of the opening chapter
This lecture mainly explains JavaScript process statements. Its general content includes the following:
Among them, the commonly used if, while, do ..while, for will not be discussed in this article. The focus will be on for..in.., label, break and continue, whth, switch and other statements
二contentarea
(1) Commonly used statements
Since the following statements are relatively common, this article will not discuss them
1. Conditional statement
if statement
2. Loop statement
while statement, do.while statement, for statement
(2) for..in.. statement
1. Definition
for...in... is an iterative statement used to enumerate objects attribute, its syntax is defined as:
for (propName in expression) statement
Based on the principle of "If you can use local variables, don't use global variables" in JavaScript development, it is recommended to define the propName attribute as a local variable, as follows:
for (var propName in expression) statement
2. Notes
(1)for....in is used to enumerate object properties, not to enumerate object property values.
Example 1:
In the following example, for..in.. outputs the array index (that is, the array attribute), not the array index value.
var i = 5; var arr = new Array(); for (var n = 0; n <p>Example 2:</p><p>In the following example, for..in.. outputs the attributes (name, age, address) of the object userInfo, but not the attribute values (Alan_beijing,38,china -shanghai)</p><pre class='brush:php;toolbar:false;'>var userInfo = { name: 'Alan_beijing', age: 38, address: 'china-shanghai' }; for (var property in userInfo) { alert(property);//name,age,address5 }
(2)for..in..enumeration properties, there is no definite order, different browsers will have differences.
(3) Before the ECMAScript5 version, if the value of the iterated object variable is null or undefined, the for statement will throw an error. After ECMAScript5, this situation will not throw an error, but the loop body will not be executed.
(3) label
1. Definition
In JavaScript, the label statement represents a label statement, which is usually used with a loop statement to represent a loop statement. Jump to the specified location.
1 label:statement
Example 1:
The following code contains a label statement outermost, whose content is two nested loop bodies. When the loop body is executed to 1==5 and j==5 , the break statement will jump to the outermost statement to continue execution.
var num = 0; outermost: for (var i = 0; i < 10; i++) { for (var j = 0; j < 10; j++) { if (i == 5 && j == 5) { break outermost; } num++ } } alert(num);//55
(4) break and continue
1. Definition
Both break and continue are expressed in the loop body and exit the loop according to specific conditions body, but there is a difference between the two. break means exiting the entire loop body, and continue means exiting the loop body that meets the conditions.
Example 1:
The following code will exit the entire loop when i=5 is executed.
var num = 1; for (var i = 1; i < 10; i++) { if (i % 5 == 0) { break; } num++; } alert(num);//5
Example 2:
The following code, when executing i=5, exits this loop, then returns to the beginning of the for statement and continues execution.
var num = 1; for (var i = 1; i < 10; i++) { if (i % 5 == 0) { continue; } num++; } alert(num);//9
2. Notes
(1) When break and continue jump out of the loop body, it means that only the immediate loop body will be jumped out, not other loop bodies except the direct loop body.
Example 1:
In the following example, break only jumps out of the direct loop body
var num = 0; for (var i = 0; i < 10; i++) { for (var j = 0; j < 10; j++) { if (i == 5 && j == 5) { continue; } num++ } } alert(num);//992.break and continue are generally used in conjunction with label statements to indicate jumping to the specified location Example:The following code, when executed When i=5 && j==5, jump to the label statement outermost and continue execution. It should be noted here that JavaScript does not have a block-level scope, so the variable i can be accessed outside the for statement
var num = 0; outermost: for (var i = 0; i < 10; i++) { for (var j = 0; j < 10; j++) { if (i == 5 && j == 5) { break outermost; } num++ } } alert(num);//55
(5) with
1. Definition The with statement sets the code scope to a specific object. Its main purpose is to simplify writing the same object multiple times and improve reuse.1 with (expression) statementExample: The following code defines a function to obtain user information, creates a new person object in the function body, and defines two attributes (name and address), and then uses the person object with stand up.
function GetUserInfo() { var person = new Object(); person.name = "Alan_beijing"; person.address = "China-shanghai"; with (person) { return name +","+ address; } } alert(GetUserInfo());//Alan_beijing,China-shanghai2. Notes(1) In JavaScript development, use the with statement with caution. There are two main reasons: one is that the with statement affects performance; the other is that the with statement is strictly prohibited In mode, an error will occur(2)The with statement encloses the public object, thereby improving code simplicity and code reusability(3)When searching for variables in the body of the with statement , first check whether the variable you are looking for exists in the body of with. If it does not exist, then check whether the variable enclosed with with has the attribute you are looking for. The following examples better reflect this principle:
function GetUserInfo() { var person = new Object(); person.name = "Alan_beijing"; person.address = "China-shanghai"; person.age = 35; with (person) { var sex = "男"; var age = 40; return name + "," + sex + "," + age +","+ address; } } alert(GetUserInfo());//Alan_beijing,男,40,China-shanghai
(6) switch
1. Define the switch statement It is what we usually call a switch statement. It is very suitable for multi-condition situations.switch (expression) { case value: statement break; case value: statement break; default:statement }Example:
如下代码,根据城市名称,查询城市类别
//根据不同城市,判断其属于几线城市 function CityType(address) { switch (address) { case "Shanghai": alert("中国一线城市"); break; case "Shenzhen": alert("中国一线城市"); break; case "Beijing": alert("中国一线城市"); break; default: alert("中国非一线城市"); } } CityType("Shenzhen");//中国一线城市
2 注意点
(1)switch本质与if是一样的,都是解决多条件多分支问题;
(2)使用switch语句的真正目的是避免使用过多的if..else if ...else....语句;
三 总结
本篇文章主要结合代码介绍了JavaScript的流程语句及其使用,重点结束了with,switch,for...in..,label,break和continue等语句,需要注意的是,在JavaScript中,流程语句都没有块级作用域,至于什么是块级作用域,将在接下来的文章中与大家分享。
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关教程请访问JavaScript视频教程!
相关推荐:
The above is the detailed content of Detailed explanation of process statements in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment