A
A
Andrey Sanych2017-09-19 09:56:25
JavaScript
Andrey Sanych, 2017-09-19 09:56:25

Code style for Javascript?

Hello. There was a question about indentation in javascript code: is there any standard that needs to be followed?
Some sources, including https://www.w3schools.com/js/js_conventions.asp , say to use 4 spaces for indentation, but most for 2 spaces ( https://github.com/airbnb/javascript )
In many frameworks and in most js code I saw exactly 2 spaces. I would like to clarify the situation. What to do? For example, PHP has PSR and there are no questions at all.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
P
Pavel Kornilov, 2017-09-19
@KorniloFF

4 spaces - imitation of a tabulator. Therefore, I separate with tabs and do not suffer. It turns out less characters in the code than even 2 spaces.

Z
zorca, 2017-09-19
@zorca

PHP also has several standards. For example the Wordpress Codex says :

Use real tabs and not spaces, as this allows the most flexibility across clients.

It doesn't mean anything, just use the type of indentation that is comfortable for you and best suited for the current project. For me, JS code is much more readable with 2 spaces.
If you really want to follow the best practices, then just open the most famous JS projects and see what indentation they use:
https://github.com/facebook/react/blob/master/.edi...
https://github .com/vuejs/vue/blob/dev/.editorconfig
https://github.com/select2/select2/blob/master/.ed... Still have
questions? )))

I
Igor Bezlepkin, 2017-09-19
@iBezlepkin

When you do spaces, there is a jamb when opening a file in different IDEs or editors. For example, when I open it through Coda2, everything floats if there are spaces.

D
Danakt Frost, 2017-09-19
@Danakt

There have been many attempts to standardize code in javascript, the most popular recommendation now is JS Sandard -
https://standardjs.com . She recommends using 2 spaces as an indentation. I personally use 4 - it's easier for the eyes to navigate the code. But I think it's a matter of taste. The indentation is easy to set up. For example, there is such a thing as editorconfig , which is supported by many popular IDEs and editors. With its help, in projects, you can easily change the settings that are convenient for a particular developer.

E
Egor Zhivagin, 2017-09-19
@Krasnodar_etc

Welcome to the "eternal holivar JS" . Next stop "Angular vs React".
Whichever is more convenient for you) I prefer 4, but in general, it's all the same. Just press tab and it will work.

N
Negwereth, 2017-09-19
@Negwereth

Specifically, on this issue, my position is - "Whoever wants, jerks off. But on the scale of the team."

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question