# Eloquent JavaScript 3rd edition (2018) 第一章~Values, Types, and Operators --- tags: Javascript relate --- ###### tags: `Javascript` > 在電腦的世界中表達13: > > 00001101 > > ![](https://i.imgur.com/qTjMqK9.png) --- # Values > To be able to work with such quantities of bits without getting lost, we must separate them into chunks that represent pieces of information. In a JavaScript environment, those chunks are called values. 這段說明了value代表了大量資訊的片段 # Numbers 一般整數 13 小數 9.81 極大或是極小的數 2.998e8 That is 2.998 × 108 = 299,800,000. # Arithmetic 加減乘除 還有一個是模除比較特別它的符號是這個(%) # Special numbers 這兩個的運算結果不是很可信 Infinity -Infinity NaN (not a number) 當運算沒有產生有意義的結果的時候會產生。 # Strings > Strings are used to represent text. 可以使用各種分號 ![](https://i.imgur.com/aMEcYfS.png) ```javascript= 可以有段行效果 `\n` 必需打出這樣子才可以呈現斜線的效果在string裡面 "\\n\" `"\\n\"` ``` 字串可以串接: ```javascript= "concatenate": "con" + "cat" + "e" + "nate" ``` ${} 這個加入string中可以進行運算: ![](https://i.imgur.com/YqJHdes.png) ```javascript= 並得出這個結果: half of 100 is 50 ``` * Unary operators 可以辨識value的typeof就是個Unary operators ```javascript= console.log(typeof 4.5) // → number console.log(typeof "x") // → string ``` # Boolean values 布林值很常做的事情就是比較並且分辨對錯是否,字串也可以比但是就必須牽扯到Unicode codes去看他們的數值做比較 ```javascript= console.log(3 > 2) // → true console.log(3 < 2) // → false ``` ```javascript= console.log("Itchy" != "Scratchy") // → true console.log("Apple" == "Orange") // → false ``` ```javascript= 唯一的特例自己的值跟自己不相等 console.log(NaN == NaN) // → false ``` # Logical operators 有三種: && 代表and ```javascript= console.log(true && false) // → false console.log(true && true) // → true ``` || 代表or 當左側為真的時候會回傳左側為主,左側不為真則回傳右側 ```javascript= console.log(false || true) // → true console.log(false || false) // → false ``` (!)代表 not `!true produces false, and !false gives true` ? : 代表 conditional operator > The value on the left of the question mark “picks” which of the other two values will come out. When it is true, it chooses the middle value, and when it is false, it chooses the value on the right. ```javascript= console.log(true ? 1 : 2); // → 1 console.log(false ? 1 : 2); // → 2 ``` # Empty values null and undefined 他們是可以互換使用的 # Automatic type conversion 當兩個type不相同的時候,js會自己轉換成可以操作的tpye去進行程序,然而那個轉換的結果往往不是我們想要的,這被稱為type coercion ```javascript= console.log(8 * null) // → 0 console.log("5" - 1) // → 4 console.log("5" + 1) // → 51 console.log("five" * 2) // → NaN console.log(false == 0) // → true ``` 文章推薦使用 === 取代== ,因為一些js的特性很容易混淆 # Short-circuiting of logical operators 一些邏輯的操作器有這些特性像是 and / or 都有 他只需要看一邊的值就可以不需要看下一個值去判斷竟可以使迴圈縮短 # Summary JavaScript values in this chapter: 1. numbers 2. strings 3. Booleans 4. undefined values Such values are created by typing in their name (true, null) or value (13, "abc"). You can combine and transform values with operators. We saw binary operators for arithmetic `(+, -, *, /, and %)`, string concatenation `(+)`, comparison `(==, !=, ===, !==, <, >, <=, >=), ` and logic `(&&, ||)`, as well as several unary operators (- to negate a number, ! to negate logically, and typeof to find a value’s type) and a ternary operator `(?:)` to pick one of two values based on a third value.