Prettier if else new line

Prettier if else new line

When using Prettier to format your code, you can achieve the desired behavior for if-else statements with new lines by configuring the “brace-style” option.

By default, Prettier uses the “stroustrup” style, which places the opening curly brace on the same line as the if statement. To add a new line before the else statement, you need to change the “brace-style” option to “1tbs”.

Here’s an example to illustrate this:

// Prettier configuration in .prettierrc file
{
  "braceStyle": "1tbs"
}

// Before formatting with Prettier
if (condition) {
  // code block
} else {
  // code block
}

// After formatting with Prettier
if (condition)
{
  // code block
}
else
{
  // code block
}

In the example above, the if statement and the else statement are each placed on a new line after formatting with Prettier.

Remember to adjust your Prettier configuration accordingly and run the command for formatting your code to see the desired changes.

Leave a comment