GeezerVison is showing github.com — Merge pull request #44 from hasselg/equality · hasselg/javascript@58c9117
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: SUMMARY.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
4 | 4 | * [Comments](basics/comments.md) |
5 | 5 | * [Variables](basics/variables.md) |
6 | 6 | * [Types](basics/types.md) |
| 7 | + * [Equality](basics/equality.md) |
7 | 8 | * [Numbers](numbers/README.md) |
8 | 9 | * [Creation](numbers/create.md) |
9 | 10 | * [Basic Operators](numbers/operators.md) |
|
Collapse file
Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +# Equality |
| 2 | + |
| 3 | +Programmers frequently need to determine the equality of variables in relation to other variables. This is done using an equality operator. |
| 4 | + |
| 5 | +The most basic equality operator is the `==` operator. This operator does everything it can to make determine if two variables are equal, even if they are not of the same type. |
| 6 | + |
| 7 | +For example, assume |
| 8 | +``` |
| 9 | +var foo = 42; |
| 10 | +var bar = 42; |
| 11 | +var baz = "42"; |
| 12 | +var qux = "life"; |
| 13 | +``` |
| 14 | +. |
| 15 | + |
| 16 | +`foo == bar` will evaluate to `true` and `baz == qux` will evaluate to false, as one would expect. However, `foo == baz` will *also* evaluate to `true` despite `foo` and `baz` being different types. Behind the scenes the `==` equality operator attempts to force its operands to the same type before determining their equality. This is in contrast to the `===` equality operator. |
| 17 | + |
| 18 | +The `===` equality operator determines that two variables are equal if they are of the same type *and* have the same value. With the same assumptions as before, this means that `foo === bar` will still evaluate to `true`, but `foo === baz` will now evaluate to `false`. `baz === qux` will still evaluate to `false`. |