JavaScript is the nuclear warhead of programming languages.
October 23, 2024•582 words
JavaScript is wacky.
JavaScript was invented in 10 days for Netscape Navigator in 1995 to help gain market share from Mosaic, the most popular browser at the time from the University of Illinois. Eventually, in the late 90s and early 2000s, competitors joined the competition such as Internet Explorer, Opera, and Mozilla Firefox.
However, instead of inventing their own scripting languages, those competitors also chose to use JavaScript, likely due to motivations by easing the barrier to entry to migrate from the leading browser at the time.
Over time, as the web platform has gained worldwide traction and JavaScript's monopoly over client-side webpage interactivity among it, more features were added to the language while always preserving backwards compatibility. As a result, JavaScript, the language people became comfortable with as a result of web programming, began to be used in domains it was never intended to be, at scales it was never intended to be, while preserving it's funkiest behaviors from its original 10-day inception in 1995.
Given that, here's a comprehensive list of all of the most absurd things that myself and others have found in JavaScript.
What is NaN, a.k.a "Not a Number"?
typeof(NaN)
// "number"
Really large numbers get larger!
9999999999999999999
// 10000000000000000000
Really small numbers aren't so small anymore.
parseInt(0.0000000005)
// 5
null
can be negative.
"0" == -null
// true
Sorting an array of numbers sorts them as strings.
let array = [100000, 30, 4, 1, 21]
console.log(array.sort());
// [ 1, 100000, 21, 30, 4 ]
Infinite prototype methods!
function a() { return 42; };
a.bar = 69;
console.log(a)
// [Function: a] { bar: 69 }
a.bar
// 69
a.a = a
// <ref *1> [Function: a] { bar: 69, a: [Circular *1] }
a.a.a.a.a.a.a.a.a.a.a.a.a
// <ref *1> [Function: a] { bar: 69, a: [Circular *1] }
Arrays are true-ly special.
true == []
// false
true == ![]
// false
false == []
// true
false == ![]
// true
You can "add" arrays and objects together.
[] + []
// ""
[] + {}
// [object Object]
{} + []
// 0
{} + {}
// NaN
No type errors here! It's a string (probably).
Array(3).join()
// ,,
Array(3).join("eh" + 1)
// "eh1eh1eh1"
Array(3).join(1 + "eh")
// NaNNaNNaN
The length of an empty array is...
let a = []
// undefined
a.length == a
// true
Generator functions stop before their last value.
function* count() {
yield 1;
yield 2;
return 3;
}
for (const value of count()) {
console.log(value)
}
// 1 2
Not even true
is true these days.
true == 1
// true
true === 1
// false
true + true + true
// 3
An empty object is actually a string in RegEx.
new RegExp({}).test(“mom”)
// True
new RegExp({}).test(“dad”)
// False
The reason the above works:
{} gets conveted to '[object Object]' by JS
Square brackets [] means "match any single character within the brackets"
"mom" matches the "o", but "dad" doesn't match any of the characters in "object Object"
A fork bomb in completely valid JavaScript that stalls a browser tab
/(.*.*)*^/.test(.1+.2)