Arity is the number of arguments. The arity of all
/ 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
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);
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.
/are what you think they are
%— divison remainder, or modulus. It calculates the remainder left over when one operand is divided by a second operand. E.g.:
11%5is 1, and
NaN, which stands for "not a number". E.g.:
NaNin the calculation, then the result will always be
120 + 5 / NaNis
const age = 39;
Take a look at this: 25 * 91. 25 and 91 are called operands, and the star is the multiplication operator.
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.
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.
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πr2. r is radius and π is approximately 3.14.
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
Let's put the surface area of Mars into another constant:
const surface = 4 * pi * 3390 * 3390;
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.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies: