Free JavaScript course. Sign Up for tracking progress →

JavaScript: Linter

Since we've learned to write simple programs, let's talk about the very process of writing.

The program code should be organized in a certain way so that it is sufficiently clear and easy to maintain. Special sets of rules - standards - describe different aspects of code writing. The most common standard in JavaScript is AirBnb.

In any programming language, there are utilities known as linters. They ensure the code meets the standards. For example, ESLint analyzes JavaScript code.

Take a look at the example from the previous lesson:

console.log(8/2+5 - -3 / 2); // => 10.5

Linter won't be happy about it, because several rules have been violated:

  • space-infix-ops – No space between operator and operands
  • no-mixed-operators – You can't write code that contains multiple operations in a single expression with no explicit parentheses separation

In the last lesson we recognized that such an abundance of numbers and symbols may be confusing and we decided to add parentheses purely for the sake of readability:

console.log(((8 / 2) + 5) - (-3 / 2)); // => 10.5

This code does not violate the rules, and the linter will "say nothing" as it were.

In previous exercise you probably did this:

console.log(70 * (3 + 4) / (8 + 2));

Is there any violation of the standard here?

Unfortunately, yes. This time, the * and / are in the same expression and there are no parentheses. You can solve this problem by adding additional parentheses. But at some point, too many parentheses can make the code incomprehensible again. At this point, you can divide the expression into separate parts. We will learn how to do this in the following lessons.

no-mixed-operators is just one of many rules. Other ones describe indentations, entity names, parentheses, mathematical operations, line length, and many other aspects. Each rule may seem small and insignificant, but together they form the basis of good code.

CodeBasics won't test your code with a linter now, but in your future Hexlet practice segments and in actual development, the linter will work and flag the bugs.

Instructions

Print the result of "the difference between five squared and the product of three and seven". Place the parentheses to follow the no-mixed-operators rule.

The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.


If you got stuck and don't know what to do, you can ask a question in our community