Simple data types

1. The type of conversion, the number of types of characters around into

(1) Number () string into digital

 var num='20'
 console.log(Number(num))    //20

 (2) parseInt () parseFloat () parse: parsing Int: integer Float: Decimal

The string type into digital type, and rounding:

 var num = ' 3.14159 ' 
 console.log (parseInt (num))     // 3

The decimal rounding

var num = 4.8 
 console.log (parseInt (num))     // 4

 parseFloat () into digital string type Type

var num='5.8'
 console.log(parseFloat(num))    //5.8

(3) + 'digital' type string into digital type

var num = " 5.8 " 
 console.log ( + num)

2. The type of conversion is converted into a string of digital

(1)string() 

 var num=20;
 console.log(String(num))  //'20'

(2) .toString value ()

 var num=20;
 console.log(num.toString())  //'20'

(3)+"

 var num = 20 ; 
 console.log (whether + "" )   // '20'

3 is converted into a boolean

var str='true';
 console.log(Boolean (str))  //true

 

4. unary num ++ ++ num and

num ++: after the first with a plus, memory method, ++ in the post, plus all post

var num = 20 ; 
 console.log (whether ++) // 20

++ num: first add after use, memory method, ++ former, is to add all

 var num = 20 ; 
 console.log ( ++ whether)   // 21

5.if statement

(1) if (condition) {} execution code

 var Age = 20 is ;
  IF (Age> = 18 is ) { 
   the console.log ( ' grown up ' )    // of age

(2) if (condition) {} execution code

else{     }

 var Age = 10 ;
  IF (Age> = 18 is ) { 
   the console.log ( ' grown up ' )    
 } 
 the else { 
   the console.log ( ' minor ' )   // Minor 
 }

(3)

if (condition 1) {1} execution code

else if (condition 2) {2} execution code

else if (condition 3) 3} {code execution

else {4} code execution

 var Age = 10 ;
  IF (Age < 12 is ) { 
   the console.log ( ' Child ' )     // children 
}
  the else  IF (Age> = 12 is && Age < 18 is ) { 
   the console.log ( ' adolescents ' )   
 } 
 the else { 
  Console. log ( ' adult ' )   
 }

6. The ternary operator

condition? Values: Value 2     

condition? Expression 1: Expression 2

was n1 = 10 ;
 was n2 = 20 ;
 was max = n1> n2? n1: n2 
 console.log (max)   // 20

7.switch statement switch

switch (variable) {

Value case 1: statement 1;

break;

Value case 2: Statements 2;

break;

Value case 3: Statements 3;

break;

default :: other cases;

break;

}

var str='';
switch (str){
  case '':
console.log('red')
break;
case '':
console.log('yellow')
break;
case '绿':
console.log('green')
break;
case '':
console.log('blue')
 BREAK ;
 default : 
the console.log ( ' other color ' )
 BREAK ; 
}

8.while loop

(1) while (condition) {

Code

}

var num = 1 
while (num <= 100 ) { 
  console.log (whether) 
  whether ++ 
}

(2) do {} loop

while (condition)

var num=1
do{
  console.log(num)
  num++ 
}
while(num<=100)

9.for cycle

(1) for (var i = 0; i <Num; i ++) {

Code

}

for(var i=0;i<=100;i++){
  sum+=i
}
console.log(sum)   //5050

(2) break for the end of the cycle, out of braces

for(var i=1;i<=5;i++){
  if(i==3){
    break;
  }
  console.log(i)
}

Print Results: 12

(3) continue to end the current cycle, continue to the next cycle

for(var i=1;i<=5;i++){
  if(i==3){
    continue;
  }
  console.log(i)
}

Printout: 1245

 

Guess you like

Origin www.cnblogs.com/zhaodz/p/11615962.html