Code structure
Statements
Statements 代表執行動作的語法, 通常會分行寫提高可讀性。
alert('Hello');
alert('World');
semicolons
當有斷行時分號可能會被省略,JavaScript 會自動加上分號,但並不是所有狀況都會自行加上分號,像是以下的範例就會出現問題。
alert("There will be an error")
[1, 2].forEach(alert)
因為沒有分號,JavaScript 引擎會視為一整行的 code 因此會出現錯誤。
alert("There will be an error")[1, 2].forEach(alert)
Comments
單行註解用 // 熱鍵 Ctrl+/ 多行註解用 /*...*/ 熱鍵 Ctrl+Shift+/
巢狀的註解會出現錯誤
/*
/* nested comment ?!? */
*/
alert( 'World' );
Last updated
Was this helpful?