0
# JavaScript Beautification
1
2
JavaScript code formatting with comprehensive support for modern JavaScript syntax, various brace styles, and advanced formatting options. Includes support for deobfuscation and unpacking of minified code.
3
4
## Core Imports
5
6
```javascript
7
// Import JavaScript beautifier
8
const { js_beautify } = require('js-beautify');
9
const beautify = require('js-beautify'); // beautify.js is the same as js_beautify
10
11
// Or use the short alias
12
const { js } = require('js-beautify');
13
```
14
15
ESM:
16
```javascript
17
import beautify from 'js-beautify';
18
// Use beautify.js() or just beautify() for JavaScript
19
```
20
21
## Capabilities
22
23
### JS Beautify Function
24
25
Beautifies JavaScript source code with extensive formatting control and syntax support.
26
27
```javascript { .api }
28
/**
29
* Beautify JavaScript source code
30
* @param {string} js_source_text - JavaScript code to beautify
31
* @param {Object} options - Formatting options (optional)
32
* @param {string} [options.brace_style='collapse'] - Brace placement style: 'collapse', 'expand', 'end-expand', 'none', 'preserve-inline'
33
* @param {boolean} [options.unindent_chained_methods=false] - Don't indent chained method calls
34
* @param {boolean} [options.break_chained_methods=false] - Break chained method calls across subsequent lines
35
* @param {boolean} [options.space_in_paren=false] - Add padding spaces within parentheses
36
* @param {boolean} [options.space_in_empty_paren=false] - Add a single space inside empty parentheses
37
* @param {boolean} [options.jslint_happy=false] - Enable jslint-stricter mode
38
* @param {boolean} [options.space_after_anon_function=false] - Add space before anonymous function parentheses
39
* @param {boolean} [options.space_after_named_function=false] - Add space before named function parentheses
40
* @param {boolean} [options.keep_array_indentation=false] - Preserve array indentation
41
* @param {boolean} [options.space_before_conditional=true] - Add space before conditional statements
42
* @param {boolean} [options.unescape_strings=false] - Decode printable characters encoded in xNN notation
43
* @param {boolean} [options.e4x=false] - Pass E4X xml literals through untouched
44
* @param {boolean} [options.comma_first=false] - Put commas at the beginning of new line instead of end
45
* @param {string} [options.operator_position='before-newline'] - Set operator position relative to newlines: 'before-newline', 'after-newline', 'preserve-newline'
46
* @returns {string} Beautified JavaScript code
47
*/
48
function js_beautify(js_source_text, options);
49
50
/**
51
* Get default options for JavaScript beautifier
52
* @returns {Object} Default options object with all JavaScript-specific options
53
*/
54
js_beautify.defaultOptions();
55
```
56
57
### Default Options Function
58
59
Retrieve the default configuration options for JavaScript beautification.
60
61
```javascript { .api }
62
/**
63
* Get default options for JavaScript beautifier
64
* @returns {Object} Default options object with all JavaScript-specific options
65
*/
66
js_beautify.defaultOptions();
67
```
68
69
**Usage Examples:**
70
71
```javascript
72
const beautify = require('js-beautify');
73
74
// Basic beautification
75
const uglyCode = 'function test(){var a=1;var b=2;return a+b;}';
76
const beautiful = beautify.js(uglyCode, { indent_size: 2 });
77
// Result:
78
// function test() {
79
// var a = 1;
80
// var b = 2;
81
// return a + b;
82
// }
83
84
// Advanced formatting with custom options
85
const complexCode = 'if(condition){doSomething();doSomethingElse();}else{doDefault();}';
86
const formatted = beautify.js(complexCode, {
87
indent_size: 4,
88
brace_style: 'expand',
89
space_before_conditional: true
90
});
91
92
// Chained method formatting
93
const chainedCode = 'obj.method1().method2().method3();';
94
const chainFormatted = beautify.js(chainedCode, {
95
break_chained_methods: true,
96
indent_size: 2
97
});
98
```
99
100
### Default Options
101
102
Returns the default configuration options for JavaScript beautification.
103
104
```javascript { .api }
105
/**
106
* Get default options for JavaScript beautification
107
* @returns Default JSBeautifyOptions object
108
*/
109
function defaultOptions(): JSBeautifyOptions;
110
```
111
112
**Usage Example:**
113
114
```javascript
115
const beautify = require('js-beautify');
116
117
// Get default options
118
const defaults = beautify.js.defaultOptions();
119
console.log(defaults.indent_size); // 4
120
console.log(defaults.brace_style); // 'collapse'
121
122
// Modify defaults for custom configuration
123
const myOptions = {
124
...defaults,
125
indent_size: 2,
126
space_after_anon_function: true
127
};
128
```
129
130
## Brace Style Options
131
132
### Collapse (Default)
133
Places opening braces on the same line as control statements.
134
```javascript
135
if (condition) {
136
statement;
137
}
138
```
139
140
### Expand
141
Places opening braces on new lines.
142
```javascript
143
if (condition)
144
{
145
statement;
146
}
147
```
148
149
### End-Expand
150
Like expand, but places closing braces on separate lines.
151
```javascript
152
if (condition)
153
{
154
statement;
155
}
156
```
157
158
### None
159
Attempts to keep braces where they are.
160
161
### Preserve-Inline
162
Preserve inline block braces and formatting (can be combined with other styles).
163
164
## Operator Position Options
165
166
### Before-Newline (Default)
167
Places operators before line breaks.
168
```javascript
169
const result = value1 +
170
value2 +
171
value3;
172
```
173
174
### After-Newline
175
Places operators after line breaks.
176
```javascript
177
const result = value1
178
+ value2
179
+ value3;
180
```
181
182
### Preserve-Newline
183
Preserves existing operator positioning.
184
185
## Special Features
186
187
### JSLint Compatibility
188
When `jslint_happy` is enabled, the formatter makes adjustments to be compatible with JSLint requirements:
189
- Forces `space_after_anon_function` to true
190
- Adjusts various formatting rules to match JSLint expectations
191
192
### E4X Support
193
When `e4x` is enabled, E4X (ECMAScript for XML) literals are passed through untouched, preserving their formatting.
194
195
### String Unescaping
196
When `unescape_strings` is enabled, the beautifier will decode printable characters that were encoded in xNN notation.
197
198
### Array Indentation
199
When `keep_array_indentation` is enabled, the original indentation of arrays is preserved even when it doesn't match the configured indent settings.