What Is the Explanation for These Bizarre Javascript Behaviours Mentioned in the ‘wat’ Talk for Codemash 2012?

What is the explanation for these bizarre JavaScript behaviors mentioned in the ‘Wat’ talk for CodeMash 2012?

JavaScript is a powerful and versatile programming language, but it can also exhibit some unexpected and bizarre behaviors. In the famous ‘Wat’ talk by Gary Bernhardt at CodeMash 2012, several of these strange behaviors were highlighted, leaving many developers scratching their heads. In this article, we will explore the explanations behind these bizarre JavaScript behaviors and shed some light on why they occur.

1. NaN is not equal to itself

One of the bizarre behaviors mentioned in the ‘Wat’ talk is that NaN (Not-a-Number) is not equal to itself. This can be quite confusing, as in most programming languages, equality between two identical values should return true. However, in JavaScript, NaN is considered to be not equal to itself.

The explanation for this behavior lies in the IEEE 754 standard for floating-point numbers, which JavaScript follows. According to the standard, NaN is considered unordered and does not compare equal to any other value, including itself. This behavior is intentional and is designed to handle scenarios where NaN may arise, such as mathematical operations involving undefined or invalid values.

Code snippet:

const result = NaN === NaN;
console.log(result); // Output: false

2. Adding arrays with the plus operator

In JavaScript, the plus operator (+) is used for both addition and string concatenation. However, when it comes to adding arrays using the plus operator, the behavior can be quite bizarre.

Instead of performing a mathematical addition or concatenation, the plus operator converts the arrays to strings and concatenates them. This can lead to unexpected results, especially when the arrays contain numbers.

Code snippet:

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const result = array1 + array2;
console.log(result); // Output: 1,2,34,5,6

3. The typeof null is ‘object’

Another bizarre behavior mentioned in the ‘Wat’ talk is that the typeof operator returns ‘object’ for null values. This can be quite surprising, as null is often considered to be a primitive value.

The reason behind this behavior is a historical mistake in JavaScript. In the early days of the language, the typeof operator was designed to return a string indicating the type of a value. However, due to an error in the implementation, null was incorrectly classified as an object. This mistake has been preserved for backward compatibility reasons, and changing it now would break existing code.

Code snippet:

const result = typeof null;
console.log(result); // Output: object

These are just a few examples of the bizarre behaviors in JavaScript mentioned in the ‘Wat’ talk. Understanding the reasons behind these behaviors can help developers avoid potential pitfalls and write more robust code. JavaScript’s quirks and oddities may seem strange at first, but with a deeper understanding, they can be tamed and leveraged to create powerful applications.


Posted

in

, ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *