TIL coding
  • Initial page
  • 排版
  • Flexbox
  • Grid
  • jQuery
  • Untitled
  • JavaScript
    • An Introduction to JavaScript
    • Hello, world!
    • Code structure
    • The modern mode, "use strict"
    • Variables
    • Data types
    • Type Conversions
    • Operators
    • Comparisons
    • Interaction: alert, prompt, confirm
    • Conditional operators: if, '?'
    • Logical operators
    • Loops: while and for
    • The "switch" statement
    • Functions
    • Function expressions and arrows
    • JavaScript specials
    • Comments
    • Ninja code
    • Automated testing with mocha
    • Polyfills
    • Objects
    • Garbage collection
    • Symbol type
    • Object methods, "this"
    • Object to primitive conversion
    • Constructor, operator "new"
    • Methods of primitives
    • Numbers
    • Strings
    • Arrays
    • Array methods
    • Iterables
    • Map, Set, WeakMap and WeakSet
    • Object.keys, values, entries
    • Destructuring assignment
    • Date and time
    • JSON methods, toJSON
    • Recursion and stack
    • Rest parameters and spread operator
    • Closure
    • The old "var"
    • Global object
    • Function object, NFE
    • The "new Function" syntax
    • Scheduling: setTimeout and setInterval
    • Decorators and forwarding, call/apply
    • Function binding
    • Currying and partials
    • Arrow functions revisited
    • Property flags and descriptors
    • Property getters and setters
    • Prototypal inheritance
    • F.prototype
    • Native prototypes
    • Prototype methods, objects without __proto__
    • The “class” syntax
    • Class inheritance
    • Static properties and methods
    • Private and protected properties and methods
    • Extending built-in classes
    • Class checking: "instanceof"
    • Mixins
    • Error handling, "try..catch"
    • Custom errors, extending Error
    • Introduction: callbacks
    • Promise
    • Promises chaining
    • Error handling with promises
    • Promise API
  • Bootstrap
    • Navbar
Powered by GitBook
On this page
  • Statements
  • semicolons
  • Comments

Was this helpful?

  1. JavaScript

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' );
PreviousHello, world!NextThe modern mode, "use strict"

Last updated 5 years ago

Was this helpful?