首行縮排2個空格 eslint: indent
functionhello (name) { console.log('hi', name)} 字串使用單引號(除了避免轉義) eslint: quotes
console.log('hello there')$("<div class='box'>") 禁止出現未使用的變數 eslint: no-unused-vars
functionmyFunction () { var result =something() // ✗ avoid} 關鍵字後增加一個空格 eslint: keyword-spacing
if (condition) { ... } // ✓ ok
if(condition) { ... } // ✗ avoid 函式宣告的圓括弧前增加一個空格 eslint: space-before-function-paren
functionname (arg) { ... } // ✓ ok
functionname(arg) { ... } // ✗ avoid
run(function () { ... }) // ✓ ok
run(function() { ... }) // ✗ avoid 使用嚴格運算子===替代相等運算子== 除了obj == null 允許檢查 null || undefined eslint: eqeqeq
if (name === 'John') // ✓ okif (name == 'John') // ✗ avoid
if (name !== 'John') // ✓ okif (name != 'John') // ✗ avoid
中綴運算子必須使用一個空格分開 eslint: space-infix-ops
// ✓ okvar x = 2var message = 'hello, ' + name + '!'
// ✗ avoidvar x=2var message = 'hello, '+name+'!'
逗號後面緊跟一個空格 eslint: comma-spacing
// ✓ okvar list = [1, 2, 3, 4]function greet (name, options) { ... }
// ✗ avoidvar list = [1,2,3,4]function greet (name,options) { ... } else和後括弧保持在同一行 eslint: brace-style
// ✓ okif (condition) { // ...} else { // ...}
// ✗ avoidif (condition) { // ...}else { // ...} 多行if聲明,使用花括弧 eslint: curly
// ✓ okif (options.quiet !== true) console.log('done')
// ✓ okif (options.quiet !== true) { console.log('done')}
// ✗ avoidif (options.quiet !== true) console.log('done') 函數參數err必須處理 eslint: handle-callback-err
// ✓ okrun(function (err) { if (err) throw err window.alert('done')})
// ✗ avoidrun(function (err) { window.alert('done')}) 必須使用瀏覽器全域參數window.首碼,除了document,console,navigator eslint: no-undef
window.alert('hi') // ✓ ok 多個空白行是不允許的 eslint: no-multiple-empty-lines
// ✓ okvar value = 'hello world'console.log(value)
// ✗ avoidvar value = 'hello world'console.log(value)
使用多行書寫三元運算式時,?和:放在所屬行 eslint: