Зарегистрируйтесь для доступа к 15+ бесплатным курсам по программированию с тренажером

Arity is the number of arguments. The arity of all `+`

, `-`

, `*`

and `/`

is 2, so we call these operators *binary*.

There is another operator that looks like binary `-`

, but it's *unary*. This is when `-`

denotes a negative number, as in `-327`

.

There could be *ternary* operators, but you don't see them often. You can read more about Arity on wikipedia.

Associativity (or fixity) determines how operators are grouped in the absence of parentheses. Consider the expression `a ~ b ~ c`

. If the operator `~`

has left associativity, this expression would be interpreted as `(a ~ b) ~ c`

. Read more about associativity on wikipedia.

Consider the following code:

```
const a;
console.log(a);
```

What is going to be printed? Well, modern JavaScript wouldn't actually allow you to create a constant without a value, but if it did, the resulting console.log would print `undefined`

. This is a special identifier.

You can set the value to `undefined`

yourself, like this:

```
const a = undefined;
```

But you shouldn't ever really do it yourself.

- Math is JavaScript looks and feels like regular math:
`+`

,`-`

,`*`

,`/`

are what you think they are- There is also
`%`

— divison remainder, or modulus. It calculates the remainder left over when one operand is divided by a second operand. E.g.:`11%5`

is 1, and`10%2`

is 0. - There are also
`Infinity`

and`-Infinity`

- When some calculation ends up in not being a number, JavaScript uses
`NaN`

, which stands for "not a number". E.g.:`0/"word"`

is`NaN`

- If there is
`NaN`

in the calculation, then the result will always be`NaN`

. E.g.:`120 + 5 / NaN`

is`NaN`

.

- If there is

- Make computer "remember" something by creating a constant. E.g.:
`const age = 39;`

- Floating Point Numbers - Computerphile. This is a great explanation of floating point number problem (the "weird problem" from the video).

- What Every Programmer Should Know About Floating-Point Arithmetic or Why don’t my numbers add up?. A special website dedicated to the "weird problem" with floating point numbers in computers.

- Decimal to IEEE754 number converter. IEEE754 is a technical standard for floating-point computation used in computers.
- Arithmetic operators in JavaScript / Mozilla Developer Network
- Infinity in JavaScript / Mozilla Developer Network
- NaN in JavaScript / Mozilla Developer Network
- What Every Computer Scientist Should Know About Floating-Point Arithmetic: long and detailed explanation with formulas

Computers obviously are really good at calculating stuff with numbers. And when it comes to simple math, JavaScript is pretty straight forward. There are five main operators: addition, substraction, multiplication, divison and divison remainder, or modulus. There are also brackets, just like in regular math, and they help explicitly specify the priority.

Take a look at this: 25 * 91. 25 and 91 are called **operands**, and the star is the multiplication **operator**.

Here's a bit more complex example: ((12 * 5) - 4) / 12 Firstly, JavaScript calculates the multiplication, then substracts 4 because of the brackets, and then divides the result by 12.

All operators here are **infix** operators: they stay between operands (in this case — between numbers). There are also **prefix** operators, for example, a minus sign that denotes a negative number, and **postfix** operators, for example, quick increment x++. Don't worry about this for now.

At some point you'll face a weird problem: if you try adding 0.1 + 0.2 in JavaScript, you end up with 0.30000-many-many-zeroes-4, not 0.3. The is because computers store numbers in a different format. Deep deep down in the memory your 0.3 is never 0.3, it's a bunch of ones and zeros with particular rules, and this format is not the best to store ALL numbers like that precisely.

And this might seem stupid — why would we make computers use such a bad system? But it's not really bad or stupid. This format works well for some things and not too well for other things.

There are also some special words in JavaScript that we need to represent certain things: divide a positive number by 0 and you get "Infinity"; divide a negative number by 0 and you get "-Infinity". In your programs you can use infinities just like numbers with other operators. For example, you can add to Infinity.

Sometimes the calculation doesn't really produce a number. Divide 0 by a string and this isn't really any number. We can't just say it's nothing, it's... not a number. JavaScript has a special word for it — it's NaN which stands for "Not a Number".

Just like infinities, NaNs can be used in calculations with other math operators. But NaN kind of brings everybody down: if it's present in the calculation, the result will always be NaN.

Here is a random question: how big is Mars? Its radius is 3390 kilometers, it's almost twice as small as Earth. But we're obviously interested in living there, so the important thing for us is how much land do we get. In other words, what is the surface area of Mars?

You might remember the formula: surface area of a sphere is 4πr^{2}. r is radius and π is approximately 3.14.

So, lets do it with JavaScript:

```
4 * 3.14 * 3390 * 3390;
```

But imagine now that we need to calculate the surface area of another planet. Say, Mercury:

```
4 * 3.14 * 2440 * 2440;
```

This new code is just like the previous one, only radius is different. If we go on like this, we will have to write the value of π ourselves all the time. And this is not great - we don't want to repeat stuff in our programs.

We can make the computer "remember" what π is and use it in calculations. This mechanism is called "constants". Let's create a new constant with the value of π:

```
const pi = 3.14;
```

`const`

is a special keyword, then goes the identifier — how you name your constant, and then the equal sign and the value.

So now we can say 'pi' instead of manually entering 3.14 in calculations.

```
4 * pi * 3390 * 3390; // surface area of Mars
4 * pi * 2440 * 2440; // surface area of Venus
```

By the way, these double slashes and words in the end are **comments**: JavaScript just ignores them and they don't affect your program. We put comments for us and other people to make code easier to understand.

Let's put the surface area of Mars into another constant:

```
const surface = 4 * pi * 3390 * 3390;
```

Now `surface`

is another identifier and it stores the **result** of the calculation. How does that calculation happen then? Well, first, JavaScript has to remember what pi is and replace it with the actual number:

```
4 * pi * 3390 * 3390;
4 * 3.14 * 3390 * 3390;
```

And then multiplications happen from left to right, since we don't have any brackets:

```
4 * 3.14 * 3390 * 3390;
12.56 * 3390 * 3390;
42578.4 * 3390;
144340776;
```

We can print the result with console.log: `console.log(surface)`

. Note that this time we don't write the quotation marks. We are not printing the word "surface". This is not a string. We are printing the value of a constant named "surface".

Now it's your turn. Continue to the quiz and the exercise now. You'll see how much energy your body has, thanks to Einstein.

Oh, and if you're watching this on YouTube — we have cool interactive exercises on our website. Just follow the link in the description. There are also lecture notes, additional stuff and quizzes, all for free. And I'll be happy to answer any of your questions right there on the website. Thank you!

The Hexlet support team or other students will answer you.

Ошибки, сложный материал, вопросы >
###### Нашли опечатку или неточность?

###### Что-то не получается или материал кажется сложным?

###### Об обучении на Хекслете

Выделите текст, нажмите
`ctrl + enter`
и отправьте его нам. В течение нескольких дней мы исправим ошибку или улучшим формулировку.

Загляните в раздел «Обсуждение»:

- задайте вопрос. Вы быстрее справитесь с трудностями и прокачаете навык постановки правильных вопросов, что пригодится и в учёбе, и в работе программистом;
- расскажите о своих впечатлениях. Если курс слишком сложный, подробный отзыв поможет нам сделать его лучше;
- изучите вопросы других учеников и ответы на них. Это база знаний, которой можно и нужно пользоваться.

- Статья «Как учиться и справляться с негативными мыслями»
- Статья «Ловушки обучения»
- Статья «Сложные простые задачи по программированию»
- Урок «Как эффективно учиться на Хекслете»
- Вебинар «Как самостоятельно учиться»

Programming courses for beginners and experienced developers. Start training for free.

- 120 courses, 2000+ hours of theory
- 900 practical tasks in a browser
- 360 000 students

Our graduates work in companies:

Sign up or sign in