current position:Home>06. JavaScript statement
06. JavaScript statement
2022-04-29 12:42:25【Hold the [email protected]】
The journey of life , You can't use your eyes to capture every bit of life in other people's stories . With your own hands , Create your own dreams .
I am a vegetable chicken front-end lover , It's been a few months since I was on the front road , Finally, I decided to take a look at the front-end classic books , Here are my notes , I hope you can correct me .
1、 brief introduction
ECMA-262 Describes some statements ( Also known as flow control statements ), and ECMAScript Most of the grammar in is embodied in sentences .
Statements usually use one or more keywords to accomplish a given task . Statements can be simple , It can also be complicated .
2、if sentence
【 grammar 】:
if (condition) statement1 else statement2
【 Syntax parsing 】:
- there condition It can be any expression , And the result is not necessarily Boolean .ECMAScript Automatically called Boolean() Function to convert the value of this expression to a Boolean value .
- If the condition evaluates to true, Then execute the statement statement1
- If the condition evaluates to false, Then execute the statement statement2
【 Single if…else Case presentation 】:
if (i > 25) {
console.log(" Greater than 25");
}else{
console.log(" Less than or equal to 25");
}
【 Multiple if Case presentation 】:
if (i > 25) {
console.log(" Greater than 25");
} else if (i < 0) {
console.log(" Less than 0");
} else {
console.log("0-25");
}
3、do-while sentence
do-while Statement is a post test loop statement , That is, the exit condition will not be evaluated until the code in the loop body is executed
【 grammar 】:
do {
statement
} while (expression);
【 Case presentation 】:
let i = 0;
do {
i += 2;
} while (i < 10);
// as long as i Less than 10, The loop will repeat .i from 0 Start , Increment each cycle 2.
4、while sentence
while A statement is a statement that tests a loop first , That is, first detect the exit condition , Then execute the code inside the loop . therefore ,while The code in the loop body may not execute
【 grammar 】:
while(expression) statement
【 Case presentation 】:
let i = 0;
while (i < 10) {
i += 2;
}
// In this case , Variable i from 0 Start , Increment each cycle 2. as long as i Less than 10, The cycle will continue .
5、for sentence
for Statement is also a test statement first , It just adds the initialization code before entering the loop , And the expression to be executed after the loop execution
【 grammar 】:
for (initialization; expression; post-loop-expression) statement
【 Case presentation 】:
let count = 10;
for (let i = 0; i < count; i++) {
console.log(i);
}
The clearest way to write is to use let Declare iterator variables , In this way, the scope of this variable can be limited to the loop
6、for-in sentence
Is a strict iterative statement , Used to enumerate non symbolic key properties in an object
【 grammar 】:
for (property in expression) statement
【 Case presentation 】:
for (const propName in window) {
document.write(propName);
}
【 matters needing attention 】:
- for-in Statement does not guarantee the order in which object properties are returned
- If for-in The variables to iterate over the loop are null or undefined, The loop body... Is not executed .
7、for-of sentence
for-of Statement is a strictly iterative statement , Used to iterate over the elements of an iteratable object
【 grammar 】:
for (property of expression) statement
【 Case presentation 】:
for (const el of [2,4,6,8]) {
document.write(el);
}
8、break and continue sentence
break and continue Statement provides more strict control over the execution of loop code .
【 Similarities and differences 】:
- break Statement to exit the loop immediately , After the next loop of the statement
- continue Statement is also used to exit the loop immediately , But it starts again at the top of the loop
【 Case presentation 】:
let num = 0;
for (let i = 1; i < 10; i++) {
if (i % 5 == 0) {
break;
}
num++;
}
console.log(num); // 4
let num = 0;
for (let i = 1; i < 10; i++) {
if (i % 5 == 0) {
continue;
}
num++;
}
console.log(num); // 8
9、switch sentence
【 grammar 】:
switch (expression) {
case value1:
statement
break;
case value2:
statement
break;
case value3:
statement
break;
case value4:
statement
break;
default:
statement
}
【 Case presentation 】:
if (i == 25) {
console.log("25");
} else if (i == 35) {
console.log("35");
} else if (i == 45) {
console.log("45");
} else {
console.log("Other");
}
// It can be written like this :
switch (i) {
case 25:
console.log("25");
break;
case 35:
console.log("35");
break;
case 45:
console.log("45");
break;
default:
console.log("Other");
}
【 matters needing attention 】:
- To avoid unnecessary conditional judgment , It's better to add... To every condition break sentence
copyright notice
author[Hold the [email protected]],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291113110033.html
The sidebar is recommended
- Construction and development of automatic test platform based on HTTP runner
- Application of RT thread - use RT thread on stm32l051 (v. conclusion of wireless temperature and humidity sensor)
- Expose Dubbo to HTTP service
- Put the front end into the spring cloud project
- Front end learning day 4 - CSS
- Vue URL jump page with parameters
- JavaScript Chapter 13 traversal of arrays and implicit parameters of function methods
- These CSS efficiency improvement skills, you need to know!
- Beijing has opened the pilot of unmanned operation of passenger cars, and the driverless air outlet has arrived
- Test theory series - Test Case elements and design methods Part II
guess what you like
How to make the peak value of measured output voltage not less than 2V
Hexo quickly set up its own blog
Gee synthetic cloudless landsat-8 and sentry-2 data
Prototype object creation method case JavaScript specific implementation steps
YAF CLI and HTTP access modes
Simple HTML + CSS animation web page production DW simple animation web page production
The correct way for web programmers to make a pink girl series happy birthday blessing web page (HTML + CSS + JS)
HTML + CSS + JS to make simple animation web pages
HTML gourmet web page production DW static web page production div + CSS gourmet web page implementation and production
Programmer 520 Tanabata Valentine's Day confession code HTML + JS + CSS petal photo album web page template programmer confession necessary
Random recommended
- Element notify notification prompt text wrap
- Belkin: it's too early to talk about real wireless space charging
- JavaScript ES6 set (set) type* ω *
- Understand JavaScript function execution process and scope chain
- Java project: nursing home management system (java + springboot + thymeleaf + HTML + JS + MySQL)
- Java project: drug management system (java + springboot + HTML + layui + bootstrap + seals + MySQL)
- An error is reported when installing hexo during hexo deployment. What is the reason and how to solve it (tag git | keyword path)
- [front end basics] Linux command: Project Automation deployment
- Less than 100 or win Porsche, Maserati "special supply" 3.0T technical analysis
- Introduction to basic methods of math built-in objects in JavaScript
- CSS matches the nth element of the parent element
- What is the commercialization process of unmanned robotaxi after China allows driverless steering wheel?
- A lot of trouble, cook also worried: a quarter less than $8 billion
- Ajax realizes no refresh paging (no refresh paging shopping cart source code)
- [flutter topic] 101 what is flutter elements Yyds dry goods inventory
- What is the commercialization process of unmanned robotaxi after China allows driverless steering wheel?
- A lot of trouble, cook also worried: a quarter less than $8 billion
- Element acquisition of data structure c language sequence stack
- Set video label based on Vue
- CSS realizes div width adaptation, and the proportion of height and width is fixed
- How to create JavaScript custom events
- Is there any recommendation for wireless signal and wired signal power amplification circuit?
- Introduction to basic methods of math built-in objects in JavaScript
- 1000km pure electric endurance is meaningless? Then why does the car factory still try its best to extend the endurance?
- [let's implement a simple vite!] Chapter 4 - compiling single file components
- 3-11xss htmlspecialchars bypass demo
- How to conduct architecture design 𞓜 deeply reveal Alibaba cloud serverless kubernetes (2)
- Heapdump performance community special series 7: front door opener of large factory -- Application Practice of fluent
- 1、 HTML base tag
- Don't leave the crane unless necessary! A case of novel coronavirus nucleic acid positive was found in Heshan, Guangdong
- [Architect (Part 20)] self defined template of scaffold and summary of phase I
- How does JavaScript understand this algorithm
- [live review] openharmony knowledge empowerment lesson 2, phase 5 - becoming a community talent
- Understand the basic history and knowledge of HTTP
- Influence of lazy loading of Web front-end training on Web Performance
- Guangxi 2021 Guangxi landscape - blessing Lake Changshou Island
- Responsive gulp Chinese network of web front end
- Twaver-html5 basic learning (26) background
- CSS learning notes [3] floating, not separated from document flow, inheritance and stacking
- Webengine loading local html is invalid, and html is the dynamic address generated by JS, which can be solved