# JavaScript 入门 Day 5

By [aow](https://paragraph.com/@ashonthewall) · 2022-03-18

---

进度很快，基础部分应该很快就能学完了。

**Stand in Line**

这个练习举了个例子，通过function来实现array的一些传递。

    function nextInLine(arr, item) {
      arr.push(item);
      let removed = arr.shift();
      return removed;
    }
    

**Understanding Boolean Values**

另一个重要概念，boolean value。

boolean value是另外一种data type，只有两个值，true/false。

很简单，就像yes or no, 1 or 0一样。

这个二元概念也为之后的if, condition这种算法逻辑打下了基础。

**Use Conditional Logic with If Statements**

If指令是condition logic的最基本形态。

If(巴拉巴拉=true){

return 很好，满足条件;

}

return 不满足；

// { }里面是满足if条件的情况，{ }外面是不满足的情况。

    function trueOrFalse(wasThatTrue) {
      if (wasThatTrue){
        return "Yes, that was true";
      }
      return "No, that was false";
    
    }
    

**Comparison with the Equality Operator**

**Comparison with the Strict Equality Operator**

**Practice comparing different values**

**Comparison with the Inequality Operator**

**Comparison with the Strict Inequality Operator**

**Comparison with the Greater Than Operator**

**Comparison with the Greater Than Or Equal To Operator**

**Comparison with the Less Than Operator**

**Comparison with the Less Than Or Equal To Operator**

if后面的( )里面可以包括价值的判断，返回true or false这两个value。

// value的判断需要用 == 哦，两个等号。一个等号= 是赋值。

这里还有个细节， == 是模糊判断，比如 2 == “2”；虽然左边是number，右边是string，但计算机会忽略data type，给你返回true;

// 其实是js自动给你进行了type的转换。

怎么严格判断呢？用 “===”;

2 === “2”; 返回false.

相等搞明白了，不等怎么办？很简单，前面加个!, “!=” or “!==”。

一样的道理，“!=”是模糊不等， “!==”是严格不等。

然后是>, <, >=, <= ;

这些都很简单，就不一一赘述了。这几节课太水了。。。不过里面的逻辑值得记住，都是机遇true/false这个基础。

**Comparisons with the Logical And Operator**

**Comparisons with the Logical Or Operator**

如果是多段条件怎么办呢？

可以这样，

    if (num > 5) {
      if (num < 10) {
        return "Yes";
      }
    }
    return "No";
    

更简单的方法，用&&把条件合并起来。

    if (num > 5 && num < 10) {
      return "Yes";
    }
    return "No";
    

&& 是 “和”，“或”怎么表示呢？

    if (num > 10 || num < 5) {
      return "No";
    }
    return "Yes";
    

用 ||；

**Introducing Else Statements**

以上我们学习了如果if条件是true，怎么返回结果，那如果是flase呢，我们可以通过else也返回结果

    function testElse(val) {
      let result = "";
    
      if (val > 5) {
        result = "Bigger than 5";
      } else {
        result = "5 or Smaller";
      }
      return result;
    }
    
    testElse(4);
    

**Introducing Else If Statements**

Else if语句很有用，相当于可以无限增加if statement

    function testElseIf(val) {
      if (val > 10) {
        return "Greater than 10";
      } else if (val < 5) {
        return "Smaller than 5";
      } else{
        return "Between 5 and 10";
      }
    }
    
    testElseIf(7);
    

//进入if语句后，会出现越来越多的 { }，大家要仔细。当然，大部分编程工具现在都可以自动识别对应 { }的。

**Logical Order in If Else Statements**

这课其实说了一点，if else statement和所有代码一样，是从上往下执行的。所以，我们应该把更宽松的条件放在上游，更严格的条件放在下游，这样才符合逻辑。

**Chaining If Else Statements**

很好的练习，把else if 逻辑串联起来实际应用。

//当然，这是最基础的方法，后面会有更简便的方法。

    unction testSize(num) {
      // Only change code below this line
      if (num < 5){
        return "Tiny";
      } else if (num < 10){
        return "Small";
      } else if (num < 15){
        return "Medium";
      } else if (num < 20){
        return "Large";
      } else if (num >= 20){
        return "Huge";
      }
    
      return "Change Me";
      // Only change code above this line
    }
    
    testSize(7);
    

**Golf Code**

高尔夫游戏记分的应用小程序，用简单的if else就可以完成，值得完整练习一下。

好了，今天就先学到这儿，if else语句可以说是非常基础和重要的，形式很简单，以后应用非常广，可以反复练习好好掌握精髓。

对了，除了这里之外，还可以在这两个地方找到我。

[https://discord.gg/madnfts](https://discord.gg/madnfts)

[https://discord.gg/Z25p8gkZWx](https://discord.gg/Z25p8gkZWx)

---

*Originally published on [aow](https://paragraph.com/@ashonthewall/javascript-day-5)*
