Answer the question
In order to leave comments, you need to log in
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
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.
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.
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.
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.
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question