JavaScript Formatter

Parses and pretty prints valid JavaScript code. Also works with JavaScript Object Notation (JSON).

Input: TextResult: Text

Text input

Tool options

Examples

Read an integer-cents checkout total

Input

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);
}
Indent string
Quotes
Auto
Semicolons before closing braces
On
Include comments
On
Inspect Fahrenheit conversion and its zero-degree boundary

Input

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

Expected answer

function celsius(f) {
    return (f - 32) * 5 / 9;
}
Indent string
Quotes
Auto
Semicolons before closing braces
On
Include comments
On
Switch an API message to double-quoted strings

Input

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

Expected answer

function message(name) {
  return "Hello, " + name + "!";
}
Indent string
Quotes
Double
Semicolons before closing braces
On
Include comments
On
Keep a note explaining a longitude-latitude pair

Input

// 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
  ]
};
Indent string
Quotes
Single
Semicolons before closing braces
On
Include comments
On
Remove a stale comment while keeping a leap-year check

Input

/* 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)
}
Indent string
Quotes
Auto
Semicolons before closing braces
Off
Include comments
Off

Parameters

ParameterDefault valueInput and choices
Indent string"\\t"Escape sequences such as \n, \r and \t are supported
QuotesAutoAuto, Single, Double
Semicolons before closing bracestrueOn or off
Include commentstrueOn or off

Related tools

Browse code tidy tools