Javascript conditional条件状态
conditional statements
conditional structures
You can have it on one condition...!
Conditional statements allow us to represent such decision making in JavaScript, from the choice that must be made, to the resulting outcome of those choices
Mom scolded me for eating all the cookies
Pseudocode
伪代码
code-like syntax
conditional structures
You can have it on one condition...!
Conditional statements allow us to represent such decision making in JavaScript, from the choice that must be made, to the resulting outcome of those choices
Mom scolded me for eating all the cookies
Pseudocode
伪代码
code-like syntax
if (condition) { code to run if condition is true } else { run some other code instead }
As a final point, you may sometimes see if...else statements written without the curly braces, in the following shorthand style:
if (condition) code to run if condition is true else run some other code instead
This is perfectly valid code, but using it is not recommended — it is much easier to read the code and work out what is going on if you use the curly braces to delimit the blocks of code, and use multiple lines and indentation.
Any value that is not false, undefined, null, 0, NaN, or an empty string ('') actually returns true when tested as a conditional statement
Logical operators : AND, OR and NOT
AND && OR || NOT !
the NOT operator will negate it.
switch (expression) { case choice1: run this code break; case choice2: run this code instead break; // include as many cases as you like default: actually, just run this code }
Here we've got:
- The keyword switch, followed by a set of parentheses.
- An expression or value inside the parentheses.
- The keyword case, followed by a choice that the expression/value could be, followed by a colon.
- Some code to run if the choice matches the expression.
- A break statement, followed by a semi-colon. If the previous choice matches the expression/value, the browser stops executing the code block here, and moves on to any code that appears below the switch statement.
- As many other cases (bullets 3–5) as you like.
- The keyword default, followed by exactly the same code pattern as one of the cases (bullets 3–5), except that default does not have a choice after it, and you don't need to break statement as there is nothing to run after this in the block anyway. This is the default option that runs if none of the choices match.
const select = document.querySelector('select'); const list = document.querySelector('ul'); const h1 = document.querySelector('h1'); select.onchange = function() { const choice = select.value; // ADD CONDITIONAL HERE if(['Juanuary','March','May', 'July', 'August', 'October', 'December'].indexOf(select.value) > 0){ createCalendar(31,select.value); } else { createCalendar(30,select.value); } } function createCalendar(days, choice) { list.innerHTML = ''; h1.textContent = choice; for (let i = 1; i <= days; i++) { const listItem = document.createElement('li'); listItem.textContent = i; list.appendChild(listItem); } } createCalendar(31,'January');
阅读量: 505
发布于:
修改于:
发布于:
修改于: