Skip to content

Commit 741916f

Browse files
Update introduction.md
1 parent fccfddc commit 741916f

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

concepts/type-checking/introduction.md

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
# About
22

3-
Knowning what type an object has is often very important for code to run smoothly and without errors.
3+
Knowning what the type of a piece of data is, is often very important for code to run smoothly and without errors.
44

5-
Javascript has several ways to check the type of an object.
5+
Javascript has several ways to check the type of a value or object.
66

7-
````exercism/note
7+
```exercism/note
88
Javascript's type checking mechanisms can be somewhat unreliable.
99
10-
For better type safety and stronger types, you should probably use TypeScript, a language that builds on JavaScript, but with the type syntax of a static-typed language.```
10+
For better type safety and stronger types, you should probably use TypeScript, a language that builds on JavaScript, but with the type syntax of a static-typed language.
11+
```
1112

1213
## The `typeof` operator
1314

14-
The `typeof` operator returns the type of its input.
15-
The output is restricted to one of the [primitive data types][primitives], `"function"` or `"object"`.
15+
The `typeof` operator returns the type of its operand.
16+
The output is a string matching the name of one of the [primitive data types][primitives], except for `"null"`.
17+
It can also be `"function"` or `"object"`.
1618

1719
```javascript
1820
typeof undefined;
@@ -37,25 +39,27 @@ typeof [1, 2, 3, 4];
3739

3840
typeof { city: 'Stockholm', country: 'Sweden' };
3941
// => "object"
40-
````
42+
```
4143

42-
The one exception to this is that `typeof null` returns `"object"` for [historical reasons][typeof null is object].
44+
For [historical reasons][`typeof null` is `"object"`].
4345

4446
## The `instanceof` operator
4547

4648
For checking the type of an object, you can use the `instanceof` operator.
47-
It returns a boolean depending on whether the second operand is included in the first operands' [prototype chain][prototype chain].
49+
It evaluates into a `boolean` depending on whether the second operand is included in the first operands' [prototype chain][prototype chain].
4850
To clarify, `instanceof` will return whether the first operand is an instance of second operand or one of its child classes.
49-
`instanceof` only works for compound data types, such as arrays and objects.
51+
`instanceof` only works on objects.
5052

5153
```javascript
5254
class Beverage {
5355
// ...
5456
}
57+
5558
// The Coffee class is a child of the Beverage class.
5659
class Coffee extends Beverage {
5760
// ...
5861
}
62+
5963
const java = new Coffee();
6064

6165
java instanceof Coffee;
@@ -68,27 +72,40 @@ java instanceof Beverage;
6872
```exercism/advanced
6973
The `Array` class has a method called `Array.isArray()` that checks if its argument is an array.
7074
71-
While `instanceof Array` will not work with an array created in a different `iframe` in a webpage, `Array.isArray()` will.
75+
While `instanceof Array` will not work with an array created in a different realm such as an `iframe` in a webpage, `Array.isArray()` will.
7276
73-
This is because the Array class has a different constructor in each `iframe`, meaning that the function in the prototype chain will be different, causing `instanceof Array` to fail.
77+
This is because the Array class has a different constructor in each realm, and each `iframe` has its own ream, meaning that the function in the prototype chain will be different, causing `instanceof Array` to fail.
7478
`Array.isArray()` is capable of ignoring this, and should always be used when possible.
79+
80+
It can also survive false positives where an object isn't actually an `Array`, and merely has `Array` in its prototype chain.
81+
82+
```javascript
83+
({ __proto__: Array.prototype }) instanceof Array
84+
// => true
85+
86+
Array.isArray({ __proto__: Array.prototype })
87+
// => false
88+
```
7589
```
7690
7791
## The `in` operator
7892
7993
The `in` operator returns whether the first operand is a property of the second operand.
80-
It does not check that the property is defined, a property set to `undefined` will still be detected by `in`.
94+
It does not check that the property has a defined value.
95+
A property set to `undefined` will still be detected by `in`.
8196
8297
```javascript
8398
class Coffee {
8499
constructor() {
85100
this.temperature = 'hot';
86101
this.isDarkMatter = undefined;
87102
}
103+
88104
coolDown() {
89105
this.temperature = 'warm';
90106
}
91107
}
108+
92109
const espresso = new Coffee();
93110
94111
'temperature' in espresso;
@@ -102,26 +119,29 @@ const espresso = new Coffee();
102119
```
103120

104121
````exercism/note
105-
`in` can be slightly unreliable, as it will return `true` for inherited properties and methods.
122+
`in` will return `true` for inherited properties and methods.
123+
106124
```javascript
107125
"coolDown" in espresso
108126
// => true
109127
110128
"constructor" in espresso
111129
// => true
112130
```
113-
To avoid this, use the hasOwnProperty() method.
131+
132+
To avoid this, use `Object.hasOwn()` instead
114133
````
115134

116135
## The `Object.hasOwn()` function
117136

118-
The `Object.hasOwn()` method returns whether the specified object has _its own property_ (not inherited or a method) that matches a string.
137+
The `Object.hasOwn()` method returns whether the specified object _owns the given property_ (it is not inherited or a method).
119138

120139
```javascript
121140
class Coffee {
122141
constructor() {
123142
this.temperature = 'hot';
124143
}
144+
125145
coolDown() {
126146
this.temperature = 'warm';
127147
}

0 commit comments

Comments
 (0)