Home > Web Front-end > JS Tutorial > The wonderful use of js and operators and or operators_javascript skills

The wonderful use of js and operators and or operators_javascript skills

WBOY
Release: 2016-05-16 16:59:54
Original
1287 people have browsed it

The following question uses if else to achieve different conditions (changes in add_step). The result value of add_level is different:

Copy code The code is as follows:

var add_level = 0;
if(add_step == 5){
add_level = 1;
}
else if(add_step == 10) {
add_level = 2;
}
else if(add_step == 12){
add_level = 3;
}
else if(add_step == 15){
add_level = 4;
}
else {
add_level = 0;
}

1) The above functions can also be achieved through switch:

Copy code The code is as follows:

var add_level = 0;
switch(add_step){
case 5 : add_level = 1;
break;
case 10 : add_level = 2;
break;
case 12 : add_level = 3;
break;
case 15 : add_level = 4;
break;
default : add_level = 0;
break;

2) Javasctipt is implemented through || and &&:

Copy code The code is as follows:

var add_level = (add_step==5 && 1) || (add_step==10 && 2) || (add_step==12 && 3) || (add_step==15 && 4) || 0;

3) The second way of writing can also be abbreviated as:

Copy the code The code is as follows:

var add_level={'5':1,'10':2,'12':3,'15':4}[add_step] || 0;

A basic formula can be derived from the second way of writing:

Copy the code The code is as follows:

add_step==5 && add_level=1

is equivalent to <==>
Copy code The code is as follows:

if(add_step==5){

add_level = 1

}

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template