You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Numbers are considered equal if they have the same value.
8
+
9
+
```javascript
10
+
1==1.0;
11
+
// => true
12
+
13
+
1===1.0;
14
+
// => true
15
+
// Remember, all numbers are floating-points, so this is different syntax for
16
+
// the exact same value.
17
+
18
+
1===1n;
19
+
// => false
20
+
// Strictly checking a number against a bigint will always result in false.
21
+
22
+
1==1n;
23
+
// => true
24
+
// A number is equal to a bigint if they represent the same value.
25
+
26
+
1.0==1n;
27
+
// => true
28
+
// A number is equal to a bigint if they represent the same value.
29
+
```
30
+
31
+
There are different outcomes when comparing numbers with different types.
32
+
In general, only two operands of the type `number` can ever be _strictly equal_ (`===`), and the following can be used for _loose equality_ (`==`):
33
+
34
+
| A | B |`==`|
35
+
| ------ | --------- | --------------------- |
36
+
| Number | Undefined |`false`|
37
+
| Number | Null |`false`|
38
+
| Number | Number |`A === B`|
39
+
| Number | String |`A === ToNumber(B)`|
40
+
| Number | Boolean |`A === ToNumber(B)`|
41
+
| Number | Object |`A == ToPrimitive(B)`|
42
+
43
+
-`ToNumber(X)` attempts to convert its argument `X` to a `number` before comparison. It is equivalent to `+B` (the unary `+` operator).
44
+
-`ToPrimitive(X)` attempts to convert its object argument `X` to a primitive value, by attempting to invoke varying sequences of `X.toString` and `X.valueOf` methods on `X`.
Copy file name to clipboardExpand all lines: concepts/numbers/about.md
+17-27Lines changed: 17 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# About
2
2
3
-
There are two different kinds of numbers in JavaScript - numbers and bigints
3
+
There are two different kinds of numbers in JavaScript - numbers and "bigints"
4
4
5
5
Numbers are the most used, and represent numeric data type in the double-precision 64-bit floating point format.
6
6
@@ -16,7 +16,7 @@ let numericValue = 42;
16
16
17
17
A number literal like `42` in JavaScript code is a floating-point value, not an integer.
18
18
There is no separate integer type in common everyday use.
19
-
The `BigInt` type was not designed to replace `Number` for everyday uses.
19
+
The `bigint` type is not designed to replace the `number` type for everyday uses.
20
20
`42` is still a `Number`, not a `BigInt`.
21
21
22
22
Number may also be expressed in literal forms like `0b101`, `0o13`, `0x0A`. Learn more on numeric lexical grammar [here][lexical-grammar].
@@ -28,7 +28,7 @@ There are two built-in objects that are useful when dealing with numbers:
28
28
-[`Number`][built-in-number]: static properties for common / useful values, static methods for [type-checking][type-checking] and [type-conversion][type-conversion], instance methods for [type-conversion][type-conversion] and [formatting numbers as strings][string-formatting].
29
29
-[`Math`][built-in-math]: properties and methods for mathematical constants and functions, does **not** work with `BigInt`.
30
30
31
-
The `Number` built-in object is _also_ a global `function` that can be used to convert _almost anything_that can be represented as a `number` to a `number`. It is less forgiving then_parsing_ a string to a number.
31
+
The `Number` built-in global `object` is _also_ a global `function` that can be used to convert _almost anything_ number-like to a `number`. It is less forgiving than_parsing_ a `string` to a `number`.
32
32
33
33
```javascript
34
34
constdate=newDate('December 17, 1995 03:24:00');
@@ -45,11 +45,11 @@ There are three types of maximum (and minimum / maximum negative) values for num
45
45
-`SAFE_INTEGER`: given by `Number.MAX_SAFE_INTEGER` and `Number.MIN_SAFE_INTEGER`
46
46
47
47
Because of how numbers in JavaScript are implemented, **not** every number between `Number.MIN_VALUE` and `Number.MAX_VALUE` can be represented.
48
-
However, _every_ number between `Number.MIN_SAFE_INTEGER - 1` and `Number.MAX_SAFE_INTEGER + `**can** be represented.
48
+
However, _every_ number between `Number.MIN_SAFE_INTEGER - 1` and `Number.MAX_SAFE_INTEGER + 1`**can** be represented.
49
49
50
50
## Comparison
51
51
52
-
Numbers can be considered equal if they have the same value.
52
+
Numbers are considered equal if they have the same value.
53
53
54
54
```javascript
55
55
1==1.0;
@@ -63,31 +63,20 @@ Numbers can be considered equal if they have the same value.
63
63
1===1n;
64
64
// => false
65
65
// Strictly checking a number against a bigint will always result in false.
66
+
```
66
67
67
-
1==1n;
68
-
// => true
69
-
// A number is equal to a bigint if they represent the same value.
68
+
See [comparison][concept-comparison] for more information on comparisons in general and comparing numeric values in JavaScript.
70
69
71
-
1.0==1n;
72
-
// => true
73
-
// A number is equal to a bigint if they represent the same value.
74
-
```
70
+
## Pitfalls
75
71
76
-
Because there are different ways to do [comparison][comparison], there are different outcomes when comparing numbers with different types.
77
-
In general, only two operands of the type `number` can ever be _strictly equal_ (`===`), and the following can be used for _loose equality_ (`==`):
72
+
Becuase numbers in JavaScript are floating point numbers, all math using these values is floating point math. Therefore, in JavaScript:
78
73
79
-
| Left | Right |`==`|
80
-
| ------ | --------- | --------------------- |
81
-
| Number | Undefined |`false`|
82
-
| Number | Null |`false`|
83
-
| Number | Number |`A === B`|
84
-
| Number | String |`A === ToNumber(B)`|
85
-
| Number | Boolean |`A === ToNumber(B)`|
86
-
| Number | Object |`A == ToPrimitive(B)`|
74
+
```javascript
75
+
0.1+0.2===0.3;
76
+
// => false
77
+
```
87
78
88
-
In the above table, `ToNumber(X)` attempts to convert its argument to a number before comparison.
89
-
Its behavior is equivalent to `+A` (the unary `+` operator).
90
-
`ToPrimitive(X)` attempts to convert its object argument to a primitive value, by attempting to invoke varying sequences of `X.toString` and `X.valueOf` methods on `X`.
79
+
See [0.30000000000000004.com](https://0.30000000000000004.com/) for a brief explanation and [Appendix D](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) of Oracle's Numerical Computation Guide "What Every Computer Scientist Should Know About Floating-Point Arithmetic" for an in depth explanation.
91
80
92
81
## Related concepts
93
82
@@ -103,5 +92,6 @@ Normally these would be put in a list, but it renders better when it's next to e
Copy file name to clipboardExpand all lines: exercises/concept/freelancer-rates/.docs/instructions.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
# Instructions
2
2
3
-
In this exercise you'll be writing code to help a freelancer communicate with a project manager by providing a few utilities to quickly calculate day- and month rates, optionally with a given discount.
3
+
In this exercise you will be writing code to help a freelancer communicate with a project manager by providing a few utility functions to quickly calculate day- and month rates, optionally with a given discount.
4
4
5
5
We first establish a few rules between the freelancer and the project manager:
6
6
7
7
- The daily rate is 8 times the hourly rate;
8
8
- A month has 22 billable days.
9
9
10
-
The freelancer is offering to apply a discount if the project manager chooses to let the freelancer bill per month, which can come in handy if there is a certain budget the project manager has to work with.
10
+
If the freelancer bills the project manager per month, there is a discount applied. This can be handy if the project manager has a fixed budget.
11
11
12
12
Discounts are modeled as fractional numbers followed by a `%` (percentage) between `0.0%` (no discount) and `90.0%` (maximum discount).
0 commit comments