JS - flow control, function, array

JS flow control/function/array

branch flow control

   // 1 . if 的语法结构
        if (条件表达式1) {
    
    
            //执行语句1
        } else if(条件表达式2) {
    
    
            //执行语句2
        } else if(条件表达式3) {
    
    
            //执行语句3
        } else {
    
    
            //执行语句4
        }

Ternary expression Condition expression? Expression 1 : Expression 2
If the result of the conditional expression is true, return the value of Expression 1 If the result of the conditional expression is false, return the value of Expression 2
Can simplify the code and replace the if statement
Values ​​matched by the switch statement are congruent

switch(表达式) {
    
    
    case value1:
        执行语句1;
        break;
    case value2:
        执行语句2;
        break;
    case value3:
        执行语句3;
        break;
    // ...
    default:
        执行语句4;
}
//循环结构
//1 . for循环
 // continue 关键字  
 // break 关键字
    for(初始化变量; 条件表达式;操作表达式) {
        if(条件表达式) {
            continue; //退出本次循环,跳到操作表达式
        } else {
            执行语句
            break;    //退出整个循环
        }
    }
      // 1 . 数组(Array) :
      var arr = new Array();   //创建数组
      var arr = [1, 2, '我', '你', true];   //字面量,创建数组,数组内的数据称为数组元素
      console.log(arr);    //获取全部
      //访问  数组名[索引下标]
      console.log(arr[2]);
      // 遍历数组
      for (var i = 0;i < arr.length;i++) {
          console.log(arr[i]);
      }
      //新增数组元素
      arr[5] = 'pink';  //追加数组元素
      arr[0] = 8;     //修改
      //不能直接给数组名赋值
       // 冒泡排序法        把一系列数据按照一定顺序 排序
      var arr1 = [76,90,30,12,54,86,3,8,4];
  for(var j = 0;j <= arr1.length-1;j++) {
          for(var i = 0;i <= arr1.length-1-i;i++) {
          if(arr1[i]>arr1[i+1]) {
              var temp = arr1[i+1];
              arr1[i+1] = arr1[i];
              arr[i] = temp;
          }
      }
  }
    ```
    

A function is a code block that encapsulates a piece of code that can be called repeatedly. Purpose: Let code be reused

  1. declare function
// function 函数名() {
    
    
    //     函数体
    // }         1)function 声明函数的关键字 全部小写 

     2. 调用函数
     函数名()
    // 函数的封装
    // 函数的参数 (形参与实参)
    // function 函数名(形参1,形参2...) {
    
    

// }
// 函数名(实参1,实参2...);

The function form participates in the matching of the number of actual parameters
1. The number of actual parameters > the number of formal parameters, and the number of formal parameters is obtained
2. The number of actual parameters < the number of formal parameters, the redundant formal parameters are defined as undefined, and the final result is NaN

//函数的返回值
function 函数名() {
    
    
    return 需要返回的结果;
}
    函数名();

1) The function implements a certain function, and the final result is returned to the caller of the function function name (), which is realized by return.
2) As long as the function encounters return, the subsequent result is returned to the caller of the function
function name () = return The result
return terminates the function and
can only return one value. If separated by commas, return the last value.
If you want to return multiple values, use an array

function getResult(num1,num2,num3) {
    
    
    return [num1+num2,num1,num2,num1-num2]
}
getResult(1,2,45);

The use of arguments

function fn() {
    
    
    console.log(arguments);   //里面储存了所有传递过来的实参
    console.log(arguments.length);
    //按照数组方式遍历数组
}
fn(1,2,3);
伪数组 并不是真正意义上的数组
1 . 具有数组 length 属性 
2.按照索引的方式进行存储 
3.没有真正数组的一些方法

Guess you like

Origin blog.csdn.net/m0_63300737/article/details/122657479