This Is The Weirdest JavaScript Operator - Summary

Summary

The speaker discusses the JavaScript `in` operator, which is often misunderstood due to its unexpected behavior. The `in` operator checks whether a property exists in an object or an index exists in an array, not the actual values. This is different from the `includes` operator, which checks if a specific value exists in an array.

The speaker demonstrates this with several examples:

1. An array `names` with elements `John`, `Josh`, `Jonathan`, and `Joe`. When `console.log('Jo' in names)` is executed, it returns `false` because `Jo` is not a property or index in the array, even though `John` is an element in the array.

2. An array `array` with elements `1`, `2`, and `3`. When `console.log('one' in array)` is executed, it returns `true` because `one` is a valid index in the array.

3. An object `myCar` with properties `make` and `model`. When `delete myCar.make` is executed, the `make` property is removed from the object. When `console.log('make' in myCar)` is executed, it returns `false` because the `make` property no longer exists in the object.

4. When the value of `myCar.make` is set to `undefined`, `console.log('make' in myCar)` returns `true` because `make` is a valid index in the object.

The speaker concludes by explaining that the `in` operator checks whether an operation can be performed on an object or array, not the actual values. This is why it can return `true` for properties or indices that do not exist in the object or array.

Facts

1. The speaker has found a JavaScript operator that they consider "silly" because it doesn't behave as expected.
2. The speaker is going to demonstrate this behavior with examples involving arrays and objects.
3. The speaker declares an array named "names" with four elements: John, Josh, Jonathan, and Joe.
4. The speaker then logs "Jo" in "names" and asks for a guess on what the output will be. The correct answer is false, as "Jo" is not in the array.
5. The speaker declares another array named "array" with three elements: 1, 2, and 3.
6. The speaker then logs "one" in "array" and asks for a guess on what the output will be. The correct answer is true, as "one" is in the array.
7. The speaker declares an object with properties for make, model, and year.
8. The speaker then deletes the "make" property from the object and logs the value of "my car.make". The output is undefined.
9. The speaker then sets the value of "my car.make" to undefined and logs the value again. The output is still undefined.
10. The speaker explains that the "in" operator in JavaScript checks if an operation can be performed on an object or array, not if a value exists in the object or array.
11. The speaker demonstrates this by showing that "0 in names" returns true, even though "0" is not a value in the array.
12. The speaker explains that the "in" operator checks if an operation can be performed on an object or array, not if a value exists in the object or array.
13. The speaker concludes by stating that the "in" operator never behaves as you might expect it to.