# 奇耙的 JS 程式碼 - else statement ###### tags: `JS` ## else 後面可以接很多東西.. 根據 [ECMAScript 2020 的定義](https://www.ecma-international.org/ecma-262/#sec-if-statement): ![](https://i.imgur.com/dCfyJqI.png) 以下程式碼合法: ### else 後面接 for ```js const a = [1,2,3] if (!Array.isArray(a)) { console.log('...'); // do something... } else for (let e of a) { console.log(e) } ``` ### else 後面接 switch case ```js const a = 'a'; if (false) { console.log('...'); } else switch(a) { case 'a': console.log('yes') break; default: console.log('no') break; } ``` ### else 後面接 while ```js if (false) { console.log('...'); } else while(true) { console.log('while') break; } ``` ### else 後面接 try ```js if (false) { console.log('...'); } else try { throw 'test' } catch(e) { console.error(e) } ```