0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<h2>Important notes</h2>
1
<h2>Important notes</h2>
2
<h3>Arity</h3>
2
<h3>Arity</h3>
3
<p>Arity is the number of arguments. The arity of all +, -, * and / is 2, so we call these operators<em>binary</em>.</p>
3
<p>Arity is the number of arguments. The arity of all +, -, * and / is 2, so we call these operators<em>binary</em>.</p>
4
<p>There is another operator that looks like binary -, but it's<em>unary</em>. This is when - denotes a negative number, as in -327.</p>
4
<p>There is another operator that looks like binary -, but it's<em>unary</em>. This is when - denotes a negative number, as in -327.</p>
5
<p>There could be<em>ternary</em>operators, but you don't see them often. You can<a>read more about Arity</a>on wikipedia.</p>
5
<p>There could be<em>ternary</em>operators, but you don't see them often. You can<a>read more about Arity</a>on wikipedia.</p>
6
<h3>Assiciativity</h3>
6
<h3>Assiciativity</h3>
7
<p>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.<a>Read more about associativity</a>on wikipedia.</p>
7
<p>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.<a>Read more about associativity</a>on wikipedia.</p>
8
<h3>Undefined</h3>
8
<h3>Undefined</h3>
9
<p>Consider the following code:</p>
9
<p>Consider the following code:</p>
10
<p>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.</p>
10
<p>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.</p>
11
<p>You can set the value to undefined yourself, like this:</p>
11
<p>You can set the value to undefined yourself, like this:</p>
12
<p>But you shouldn't ever really do it yourself.</p>
12
<p>But you shouldn't ever really do it yourself.</p>
13
<h2>Lesson notes</h2>
13
<h2>Lesson notes</h2>
14
<ul><li>Math is JavaScript looks and feels like regular math:<ul><li>+, -, *, / are what you think they are</li>
14
<ul><li>Math is JavaScript looks and feels like regular math:<ul><li>+, -, *, / are what you think they are</li>
15
<li>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</li>
15
<li>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</li>
16
<li>There are also Infinity and -Infinity</li>
16
<li>There are also Infinity and -Infinity</li>
17
<li>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<ul><li>If there is NaN in the calculation, then the result will always be NaN. E.g.: 120 + 5 / NaN is NaN</li>
17
<li>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<ul><li>If there is NaN in the calculation, then the result will always be NaN. E.g.: 120 + 5 / NaN is NaN</li>
18
</ul></li>
18
</ul></li>
19
</ul></li>
19
</ul></li>
20
<li>Make computer "remember" something by creating a constant. E.g.: const age = 39;</li>
20
<li>Make computer "remember" something by creating a constant. E.g.: const age = 39;</li>
21
</ul><h2>Recommended watching</h2>
21
</ul><h2>Recommended watching</h2>
22
<ul><li><a>Floating Point Numbers - Computerphile</a>. This is a great explanation of floating point number problem (the "weird problem" from the video)</li>
22
<ul><li><a>Floating Point Numbers - Computerphile</a>. This is a great explanation of floating point number problem (the "weird problem" from the video)</li>
23
</ul><h2>Recommended reading</h2>
23
</ul><h2>Recommended reading</h2>
24
<ul><li><a>What Every Programmer Should Know About Floating-Point Arithmetic or Why don’t my numbers add up?</a>. A special website dedicated to the "weird problem" with floating point numbers in computers.</li>
24
<ul><li><a>What Every Programmer Should Know About Floating-Point Arithmetic or Why don’t my numbers add up?</a>. A special website dedicated to the "weird problem" with floating point numbers in computers.</li>
25
</ul><h2>Optional</h2>
25
</ul><h2>Optional</h2>
26
<ul><li><a>Decimal to IEEE754 number converter</a>.<a>IEEE754</a>is a technical standard for floating-point computation used in computers.</li>
26
<ul><li><a>Decimal to IEEE754 number converter</a>.<a>IEEE754</a>is a technical standard for floating-point computation used in computers.</li>
27
<li><a>Arithmetic operators in JavaScript / Mozilla Developer Network</a></li>
27
<li><a>Arithmetic operators in JavaScript / Mozilla Developer Network</a></li>
28
<li><a>Infinity in JavaScript / Mozilla Developer Network</a></li>
28
<li><a>Infinity in JavaScript / Mozilla Developer Network</a></li>
29
<li><a>NaN in JavaScript / Mozilla Developer Network</a></li>
29
<li><a>NaN in JavaScript / Mozilla Developer Network</a></li>
30
<li><a>What Every Computer Scientist Should Know About Floating-Point Arithmetic: long and detailed explanation with formulas</a></li>
30
<li><a>What Every Computer Scientist Should Know About Floating-Point Arithmetic: long and detailed explanation with formulas</a></li>
31
</ul><h2>Lesson transcript</h2>
31
</ul><h2>Lesson transcript</h2>
32
<p>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.</p>
32
<p>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.</p>
33
<p>Take a look at this: 25 * 91. 25 and 91 are called<strong>operands</strong>, and the star is the multiplication<strong>operator</strong>.</p>
33
<p>Take a look at this: 25 * 91. 25 and 91 are called<strong>operands</strong>, and the star is the multiplication<strong>operator</strong>.</p>
34
<p>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.</p>
34
<p>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.</p>
35
<p>All operators here are<strong>infix</strong>operators: they stay between operands (in this case - between numbers). There are also<strong>prefix</strong>operators, for example, a minus sign that denotes a negative number, and<strong>postfix</strong>operators, for example, quick increment x++. Don't worry about this for now.</p>
35
<p>All operators here are<strong>infix</strong>operators: they stay between operands (in this case - between numbers). There are also<strong>prefix</strong>operators, for example, a minus sign that denotes a negative number, and<strong>postfix</strong>operators, for example, quick increment x++. Don't worry about this for now.</p>
36
<p>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.</p>
36
<p>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.</p>
37
<p>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.</p>
37
<p>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.</p>
38
<p>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.</p>
38
<p>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.</p>
39
<p>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".</p>
39
<p>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".</p>
40
<p>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.</p>
40
<p>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.</p>
41
<p>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?</p>
41
<p>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?</p>
42
<p>You might remember the formula: surface area of a sphere is 4πr2. r is radius and π is approximately 3.14.</p>
42
<p>You might remember the formula: surface area of a sphere is 4πr2. r is radius and π is approximately 3.14.</p>
43
<p>So, lets do it with JavaScript:</p>
43
<p>So, lets do it with JavaScript:</p>
44
<p>But imagine now that we need to calculate the surface area of another planet. Say, Mercury:</p>
44
<p>But imagine now that we need to calculate the surface area of another planet. Say, Mercury:</p>
45
<p>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.</p>
45
<p>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.</p>
46
<p>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 π:</p>
46
<p>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 π:</p>
47
<p>const is a special keyword, then goes the identifier - how you name your constant, and then the equal sign and the value.</p>
47
<p>const is a special keyword, then goes the identifier - how you name your constant, and then the equal sign and the value.</p>
48
<p>So now we can say 'pi' instead of manually entering 3.14 in calculations.</p>
48
<p>So now we can say 'pi' instead of manually entering 3.14 in calculations.</p>
49
<p>By the way, these double slashes and words in the end are<strong>comments</strong>: 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.</p>
49
<p>By the way, these double slashes and words in the end are<strong>comments</strong>: 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.</p>
50
<p>Let's put the surface area of Mars into another constant:</p>
50
<p>Let's put the surface area of Mars into another constant:</p>
51
<p>Now surface is another identifier and it stores the<strong>result</strong>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:</p>
51
<p>Now surface is another identifier and it stores the<strong>result</strong>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:</p>
52
<p>And then multiplications happen from left to right, since we don't have any brackets:</p>
52
<p>And then multiplications happen from left to right, since we don't have any brackets:</p>
53
<p>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".</p>
53
<p>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".</p>
54
<p>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.</p>
54
<p>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.</p>
55
<p>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!</p>
55
<p>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!</p>