Skip to main content

Numbers

@serverSerrverlesskiy

There are two 2️⃣ types of numbers in modern JavaScript:

number

Ordinary numbers in JavaScript are stored in the 64-bit IEEE-754 format, also called double precision floating point numbers. These are the numbers we will use most often. Integers are not considered a separate type of numbers. In addition to floating point numbers, the numeric data type also includes three character values: Infinity, -Infinity, and NaN (non-number).

Numbers

bigInt

Numbers make it possible to work with integers of arbitrary length. They are rarely needed and are used in cases where you need to work with values ​​outside the maximum safe integer value Number.

Any number, even a decimal fraction with a lot of decimal places, is never quoted.

You can use four types of numeric literals: decimal, binary, octal, and hexadecimal. Since the last three are used quite rarely, we will omit their detailed description , but the curious can get acquainted with them here.

caution

Be careful when using leading zeros! This means you don't need to put zero before the decimal number.

1234567890
42

0888 // 888 is treated as decimal
0777 // treated as non-strict octal (511 decimal)

Arithmetic operations

Arithmetic operation

Two or more integers can be used to form a new integer. There are many ways to compose a new integer. The way to compose a new number from two or more numbers is called arithmetic. In general, there are many arithmetic operations, but there are only four basic ones: addition, subtraction, multiplication and division. They are called basic, because all other actions are brought to them.

The plus sign + is used to express addition: 4 + 4 Answer: 8

Minus for subtraction: 7 - 6 Answer: 1

The asterisk * represents multiplication: 3 * 4 Answer: 12

Forward slash / division: 15 / 5 Reply: 3

If more than one action is performed in a line, we use - (parentheses) to separate them from each other and also to make the code more readable. Let's type the following sentences into the console. The answer for each of them should consist of only one digit9️⃣:

 3 * (2 + 1)
(27 + 9) / (10 - 6)
(63 / 3 ) / (6 + 1)
2 * (5 - 8 / 2) + (3 + 4)

Enter the following values into LIVE EDITOR :

Live Editor
Result
Loading...

Combined assignment

Conbination

An operator is a symbolic designation of some action performed with operands in an expression (For example: +, -,*,/).

The operand is some value that is processed in the program. Operands can be of any data type. The operand to the left of the operator is the left operand, the operand to the right of the operator is the right operand.

The main operator of combined assignment is the equal to =, and it assigns the value of the right operand to the left one. That is - x = y assigns the value to the variable y, to the variable x.

You have seen many times how you assign values ​​to variables using mathematical operators. For example, like this:

let sum = 2 + 3 // sum value 7

And you probably did not have time to forget that at any time you can change the value of an already known variable :

Live Editor
Result
Loading...

Assignment with addition + = in order to quickly increase the value of a variable! Here are some examples:

let value = 5
value += 2 // value is now 7 (same as value = // value + 2)
value += 3 // value is now 10 (same as value = // value + 3)
value = value + value // 20 (or you can just value + = // value)
value += value // 40 (same as value = value + // value)

You already guessed that such things work with other mathematical operations, right?!

value -= 25 // value is now 15 (same as value = value - // 25)
value *= 2 // value is now 30 (same as value = // value * 2)
value /= 3 // value is now 10 (same as value = // value / 3)
value // Answer: 10

Next, check all the listed examples in LIVE EDITOR:

Live Editor
Result
Loading...

You can read more about combined assignment here

Increment and decrement

increment

The operator ++ (increment) increases the value of its operand by one. If the value of the operand is not a number, the operator automatically converts it to a number, increments it by one, and returns the result, which is assigned back to the operand:

Live Editor
Result
Loading...

The operator -- (decrement) works similarly to the increment operator, but does not increase the value of its operand, but, on the contrary, decreases it by one:

Live Editor
Result
Loading...

The modulo operator

function

The % (percent) signifies the remainder of the division. The operator returns the integer remainder of dividing the left operand by the right. The return value always gets the sign of the dividend, not the divisor. It uses the built-in function⚙️ modulo, which is the integer remainder of dividing let1 by let2.

12% 5 result 2

NaN% 2 results in NaN

1% 2 result 1

2% 3 result 2

4% 2 result 0

5.5% 2 result 1.5

Check out all the listed examples in LIVE EDITOR and you will immediately understand everything:

Live Editor
Result
Loading...

Rounding

Balls

The Math.round () method returns a number rounded to the nearest integer. If the fractional part of the number is greater than or equal to 0.5, the argument will be rounded to the nearest larger integer. If the fractional part of the number is less than 0.5, the argument will be rounded to the nearest lower integer.

result = Math.round (20.49) Returns 20

result = Math.round (20.5) Returns the value 21

check it yourself:

Live Editor
Result
Loading...

EnglishMoji!

Problems?

Problem

Write to Discord chat.

Questions:

Question

What arithmetic operations are called basic?

  1. Addition, subtraction
  2. Multiplication, division
  3. Addition, subtraction, multiplication, division

What is the correct reading of + =?

  1. Increment
  2. Assignment with addition
  3. Plus and equal

How is the increment written by sign (s)?

  1. ++
  2. --
  3. +

What is the sign for the remainder of the division?

  1. %
  2. /
  3. \

What value will Math.round return when Math.round (20.62)is applied?

  1. 22
  2. 20
  3. 21

In order to understand how much you learned this lesson, take the test in the mobile application of our school on this topic or in our telegram bot.

EnglishMoji!

  1. MDN web docs
  2. Code for Teens: The Perfect Beginner's Guide to Programming, Volume 1: Javascript - Jeremy Moritz
  3. JavaScript.ru
  4. Integer arithmetic

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Dmitriy Vasilev

💵

Resoner2005

🐛 🎨 🖋