JS basis - flow control statements grammar + + + function built-in object [] array

JS syntax:

 

Javascript basic data types: undefined / null / Boolean / number / string

Complex data types: object

typeof for detecting the data type of the variable

typeof use in two ways:

(1) typeof variable

(2) typeof (variable)

undefined derived from null, so the results are undefined == null returns true

 


 

 

Any data type, Number () can be converted to non-numerical values, but there are some data can not be converted to a numeric value, if such data conversion performed by forcibly Number (), then returns NaN

var a=Number(name_01);

For converting numeric strings

var b=parseInt("28px")
var d=parseFloat("12.34.56px");

I do not know the value is not null or undefined, you can use the String (), the value of any type can be converted to a string

var idstr=ids.toString();
console.log(String(m));

Then after the arithmetic calculation, in addition to adding the rest of the operation will first be converted into a digital string, but the two data are added, if there is one string, so the effect is to increase their number splicing

Such as: number 5 and the string "5" for arithmetic operation, the result were: 550,251

 


 

&& Logical AND:

If the first operand is implicit conversion is true, the second operand is returned;

If the previous are true, then the operation returns to the last number.

If the first operand is implicit conversion is false, the first operand is returned;

If there is a null, then return null;

If one is undefined, the return undefined;

If there is a NaN, NaN is returned

 


 

 

|| logical OR:

If the first operand is implicit conversion is true, the first operand is returned;

If the first operand is implicit conversion to false, the second operand is returned back in sequence;

If the two numbers are null, null is returned;

If the two numbers are undefined, undefined is returned;

If both numbers are NaN, NaN is returned;

 


 

 

! Logical NOT

In any case returns a Boolean value

!! results again negated logical negation;

 


 

JS flow control statements:

var Age = prompt ( "Please set your password" );
        IF (Age <18 is ) {
          Alert ( "You are not an adult" );
       }else{
          Alert ( "Older than you are, please provide your ID number" );
       }

if(age.length!=6){
          Alert ( "Please enter the 6 digit password" );
       }else{
          if(isNaN(age)){
            Alert ( "The password must be six digits" );
          }else{
            Alert ( "correct password" );
          }
       }

NaN is a number one kind of type, typeof NaN == number

var week=new Date().getDay();
       var weekstr="";
       the console.log (Week);   // 0-6 
       // Analyzing multiple switch condition 
       switch (Week) {
           Case 0 :
          weekstr = "date" ;
           BREAK ;   // Exit 
          Case 1 :
          weekstr="一";
          break;
          case 2:
          weekstr="二";
          break;
          case 3:
          weekstr="三";
          break;
          case 4:
          weekstr="四";
          break;
          case 5:
          weekstr="五";
          break;
          default:
          weekstr="六";
       }
       document.write ( "What day is today" + weekstr);

nested for loop:

Outer layer of the inner layer is not performed when false;

Performed first outer layer, the inner layer then performed; when the inner layer is false then performed.


JS function:

Stop function is executed immediately after completion and exit function return.

If there is no value following the return, return undefined default

 

 


 

Js under non-strict mode, you can use arguments passed to change the value of the parameter

function inner(){
          // arguments 
          console.log(arguments.length);
          the console.log (arguments [ . 1]); // index is a positive integer of from 0 
       }
       inner(10,5);
       function add(num1,num2){    
          arguments[0]=99;
          console.log(num1);
       }
       add(55,88)

JS built-in objects [array]:

Creating an array of two ways:

new Array()

Literal mode []

 


 

arr.length get an array length

By setting the length you can remove items from the end of the array, or add new items

 


 

Statistics wages code:

(1) non-stop user input is received wages of employees, until the user enters "Exit" will not "enter salary" pop-up window

(2) the user input data is stored in an array

var input,arr=[];
       while(input!='退出'){
            the INPUT = prompt ( 'Please enter the wages and salaries' );
            arr[arr.length]=input;
       }
       arr.length - =. 1; // not stored 'exit' 
       document.write (ARR);

Stack array of methods:

// push从数组末尾加入
       var colors=new Array("red","green");
       var len=colors.push("blue","yellow","blank");
       console.log (s);
       // the unshift added from the head array 
       var the nums = [2,7,8,6 ];
        var size = nums.unshift (99,66 );
        // POP removed from the end of the array, the return value is removed element 
       var n- = nums.pop ();
       console.log(nums);
       // Shift the head removed from the array, the return value is removed element 
       var m = colors.shift ();
       console.log(m);
// the Join the array elements into a string 
       var the nums = [2,4,5 ];
        var STR = nums.join ();   // 2,4,5 
       var words = [ "border", "left", " Color " ];
        // border-left-Color 
       var wordstr = words.join (" - " );
       console.log(wordstr);
       // Reverse elements in the array in reverse order 
       nums.reverse ();
       console.log(nums);
       var STRs = [ "A", "B", "C", "D" ];
        // Returns string dcba 
       var for newstr = strs.reverse () the Join ( "." )
       console.log(newstr);
       // 29,5,24,17,32 
       var ARR = [9,23,15, -99,88,12, -2 ];
        // Sort () array elements are sorted to sort by format string 
var = _arr1 [8,95,31,1,5 ];
         
        console.log (_arr1.sort ()); // output 1,31,5,8,95, because the first element will be converted to a string and compares it with the first character of the string, 
       // if the first character same, the comparison continues second character 
       // for the case of the above, the following solutions 
       // descending return parameter 1 <parameter 2 
       arr.sort ( function (a, B) { return a < B});
        // ascending return parameter 1> parameter 2 
       arr.sort ( function (A, B) { return A> B});
       console.log(arr);
var arr1=["a","b","c"],
          arr2=["d","e",1,3],
          arr3;
      // the concat connect two or more arrays 
      ARR3 = arr1.concat (arr2 is, [ "m", 99,8 ]);
      console.log(arr3);
      // Slice (start, End) End portion of the returned array subscript taken 
      // comprise start, does not contain End 
      // start negative, that is, the array length + length 
      // if there is no End, from the start until the end of it 
      var Colors = [ "Red", "Green", "Blue", "Yellow", "Orange" ];
       var newColors colors.slice = (l, 3 );
       var newColors2 colors.slice = (2,4 );
       var newColors3 = Colors. Slice (-4,3); // l, 3 
      the console.log (newColors3);
// Method copy arrays 
      var A = [. 1, "Yes",. 3 ],
          b;
      // 1、数组遍历,push
      b=new Array();
      for(var i=0;i<a.length;i++){
         b.push(a[i]);
      }
      // 2、concat()
      b=[].concat(a);
      // 3、slice();
      b=a.slice(0);
      console.log(b);
// splice return value is the entry is deleted, if not delete [] returns 
       var ARR = [ "A", "B", "C", "D", "E", "F" ];
        // Delete , remove from index number COUNT, return deleted 
       // splice (index, COUNT) 
       // COUNT to 0 will not be deleted; count is not provided, to remove the last from the beginning index 
       var delArr arr.splice = (2, 3 );
        // inserted, from index to count delete, insert ... ITEM1 
       @ splice (index, count, ITEM1 ...) 
       var insertArr arr.splice = (3,0, "m", "n- ", 88 );
        // replace 
       var replaceArr arr.splice = (1,2," X "," Y "," Z " );
       console.log(arr);
       console.log(replaceArr);
var the nums = [1,7,5,7,8,1,6,9 ];
        // the indexOf (seachvalue, startIndex) 
       // returned to find the location of items in the array, and returns -1 if no 
       var POS = the nums .indexOf (7,2 );
        // lastIndexOf () starts from the end of the array to find 
       var POS nums.lastIndexOf = (. 1 );
        var POS = nums.indexOf ( ". 7" );
       console.log(pos);

indexOf () method has compatibility problems, solutions:

// packaging a function implemented method indexOf 
       function ArrayIndexOf (arr, value) {
           // the detection value occurring in arr position 
          for ( var I = 0; I <arr.length; I ++ ) {
              IF (arr [I] = == value) {
                 return I;
             }
          }
          return -1;
       }

Guess you like

Origin www.cnblogs.com/chenyingying0/p/12122047.html