One of the most common examples of this is the
isNaN() function, which is used to determine if a given value is not a number. However, when used in combination with the
parseFloat() function, the result can be quite unexpected. For example, the following code returns true:
!isNaN(parseFloat('Infinity')) // true
This is because
isNaN(Infinity) returns false. This means that the ! operator negates the result, resulting in true.
Another example of unexpected behavior is the typeof operator, which is used to determine the type of a given value. However, when used with certain values, the results can be quite surprising. For example, the following code returns true:
typeof NaN === 'number' // true
Another example of unexpected behavior is the typeof operator when used with the null value, which returns “object”:
typeof null === 'object' // true
This is because null was initially considered as an object by developers and it was intended to represent no value. but later it is considered as a separate type.
Additionally, the typeof operator when used with Math, returns “object”
typeof Math === 'object' // true
Math is not a typical object, it is considered as an object.
Finally, when used with a class,
typeof returns “function”
typeof MyClass === 'function' // true
Lastly, the typeof operator when used with the
new Date() returns “object”
typeof new Date() === 'object' // true