格式化 JavaScript

重新縮進和排版源代碼以便阅读;不能代替執行驗證或业务规則檢查。

輸入: 文本結果: 文本

文本輸入

工具選項

範例

Read an integer-cents checkout total

輸入

function totalCents(items){return items.reduce(function(sum,item){return sum+item.cents*item.quantity;},0);}

Expected answer

function totalCents(items) {
  return items.reduce(function (sum, item) {
    return sum + item.cents * item.quantity;
  }, 0);
}
縮進字串
引號
Auto
在右花括號前保留分號
On
包含注释
On
Inspect Fahrenheit conversion and its zero-degree boundary

輸入

function celsius(f){return(f-32)*5/9;}

Expected answer

function celsius(f) {
    return (f - 32) * 5 / 9;
}
縮進字串
引號
Auto
在右花括號前保留分號
On
包含注释
On
Switch an API message to double-quoted strings

輸入

function message(name){return 'Hello, '+name+'!';}

Expected answer

function message(name) {
  return "Hello, " + name + "!";
}
縮進字串
引號
Double
在右花括號前保留分號
On
包含注释
On
Keep a note explaining a longitude-latitude pair

輸入

// GeoJSON positions use longitude before latitude
var point={type:"Point",coordinates:[100,0]};

Expected answer

// GeoJSON positions use longitude before latitude
var point = {
  type: 'Point',
  coordinates: [
    100,
    0
  ]
};
縮進字串
引號
Single
在右花括號前保留分號
On
包含注释
On
Remove a stale comment while keeping a leap-year check

輸入

/* old debug note */function isLeap(y){return y%4===0&&(y%100!==0||y%400===0);}

Expected answer

function isLeap(y) {
  return y % 4 === 0 && (y % 100 !== 0 || y % 400 === 0)
}
縮進字串
引號
Auto
在右花括號前保留分號
Off
包含注释
Off

参數

参數默認值輸入與選項
縮進字串"\\t"支持 \n、\r 和 \t 等跳脫序列
引號自动自动, Single, Double
在右花括號前保留分號true開啟或关閉
包含注释true開啟或关閉

相关工具

程式碼整理

操作参考

查看原始技术文檔 (English)