WebStorm 2017.2 Help

Extract Constant

Basics

The Extract Constant refactoring makes your source code easier to read and maintain. It also helps you avoid using hard-coded constants without any explanations about their values or purpose.

Example

BeforeAfter
Parenizor.method('toString', function () { return '(' + this.getValue() + ')'; }
Parenizor.method('toString', function () { const string = '(' + this.getValue() + ')'; return string; }

How to extract a constant

To extract a constant

  1. In the editor, position the cursor inside the expression to be replaced with a constant.
  2. Choose Refactor | Extract Constant on the main menu or on the context menu of the selection. Alternatively press Ctrl+Alt+C.
  3. If a constant can be extracted from several expressions in the current context, WebStorm shows all the relevant expressions in a pop-up list. Select the expression to apply the refactoring to.
  4. In the ExtractConstant dialog box, type the name of the new constant in the Name text box.
  5. Specify the scope to apply refactoring in:
    • To have only the selected expression replaced, clear the Replace all occurrences check box.
    • To have WebStorm replace the selected expression wherever it is used, select the Replace all occurrences check box.
  6. Click OK to start the refactoring.

See Also

Last modified: 10 August 2017