# Javascript 指南 [A re-introduction to JavaScript (JS tutorial)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript) ## Numbers > There's no such thing as an integer in JavaScript 表示在javascript裡沒有整數存在 ```javascript= console.log(3 / 2); // 1.5, not 1 console.log(Math.floor(3 / 2)); // 1 ``` ```javascript= 0.1 + 0.2 == 0.30000000000000004; ``` 實際上,整數值被視為32-bit ints,某些實現甚至以這種方式存儲它,直到要求他們執行對Number有效但對32-bit ints無效的指令為止。這對於在位運算可能很重要。 #### NaN ```javascript= isNaN(NaN); // true ``` #### Infinity, -Infinity ```javascript= isFinite(1 / 0); // false isFinite(-Infinity); // false isFinite(NaN); // false ``` ## Strings To find the length of a string (in code units), access its length property: ``'hello'.length; // 5`` ``` 'hello'.charAt(0); // "h" 'hello, world'.replace('world', 'mars'); // "hello, mars" 'hello'.toUpperCase(); // "HELLO" ``` ### null , undefined **null:** 表示參酌 non-value(沒有值) **undefined:** 表示未初始化的變數,還沒被分配 在Javascript中可以宣告變數沒有給參數值。如果這樣的話,變數的類型就是不確定的,undefined實際上是一個常數。 ### boolean * false,0,空字符串(“”),NaN,null和undefined都將變為false。 * 所有其他值都變為true。 ```javascript= Boolean(''); // false Boolean(234); // true ``` ### Variabled #### **let:** 允許宣告在block裡,宣告的變數可以從block中獲得。 ```javascript= for (let myLetVariable = 0; myLetVariable < 5; myLetVariable++) { // myLetVariable is only visible in here } ``` #### **const:** 宣告的值 > const allows you to declare variables whose values are **never intended to change.** The variable is available from the block it is declared in. 宣告的值沒有預期改變,也就是不會改變,該變數可以從block裡得到。 ```javascript= const Pi = 3.14; // variable Pi is set Pi = 1; // will throw an error because you cannot change a constant variable. ``` #### *var* >JavaScript, blocks do not have scope; only functions have a scope. So if a variable is defined using var in a compound statement (for example inside an if control structure), it will be visible to the entire function 如果宣告沒有給任何參數值就會是undefined。 在javascript裡block沒有作用域(scpoe),只有function有,因此如果在複合語句中使用var定義變數,像是( for(var ...) ),變數將進入function。 但是從es5開始,let和const允許宣告變數在block 作用域裡。 ### Control structures 控制結構 #### for迴圈 初始式只執行一次,所以通常用來宣告或初始變數,如果是宣告變數,結束for迴圈後變數就會消失。每次執行迴圈本體前會執行一次,且必須去true或false的結果,false的話就會結束迴圈。 #### while迴圈 > JavaScript has while loops and do-while loops. The first is good for basic looping; the second for loops where you wish to ensure that the body of the loop is executed at least once: while是基本的迴圈,當條件是成立時,程式會重複執行指令,每執行完指令一之後,便再檢查一次該條件式是否成立,如果成立,則繼續執行迴圈內的指令(也就是指令一),而如果條件式不成立了,則離開這個迴圈。 在迴圈敘述中,我們可以使用 break 指令讓它強制離開迴圈,或是用 continue 指令來忽略下面的指令而繼續下一次的迴圈,例如: ```javascript= a = ( rand()%100 ) + 1; b = 0; while ( b!=a ) { scanf("%", &b); if( b > 100 ) continue; if( b <= 0 ) break; if( b > a ) printf("Too big!\n"); if( b < a ) printf("Too small!\n"); } if( b==a ) printf("Bingo!\n"); else printf("Give up!!\n"); ``` do-while迴圈是希望確保至少要執行一次。 while 和 do-while 的語法非常像,唯一的不同是, while 是先檢查條件是否成立,成立才執行下面的指令,而 do-while 是先執行那些指令,再去檢查條件是否成立。 [迴圈敘述 - while](http://dhcp.tcgs.tc.edu.tw/c/p006.htm) #### for與while的差別 for迴圈具有初始值及終止值,其執行次數完全依判斷條件而定。 while迴圈通常沒有初始值,執行次數完全依判斷條件而定。如果要使用while迴圈取代for迴圈,可在while迴圈之前設定初始值。 ```javascript= for(i=1; i < 5; i++) { } // 以while迴圈達到相同結果 let i = 1; // 設定初始值 while(i < 5) { cout // 執行第i次迴圈 i++ } ``` :::success 雖然可以達到相同效果,但就要增加程式碼,執行效能就會比for迴圈差。 :::