JavaScript Is Broken: 5 Odd Things About JavaScript
JavaScript, Known for its fast-growing community and eco-system, it has gotten this advantage, because of its simplicity, flexibility and dynamics. Developers have been using javascript for quite a variety of fields other than the web. For these same reasons, comes the problem.
JavaScript was built by Brendan Eich. It was written to make websites more dynamic. It was written in merely 7 days. This fact has been seen by many developers to be the reason why javascript can't be fixed.
JavaScript has been one of the most hated languages by developers. It has been criticized due to its initial rush in development and the ongoing effort to patch the language. Despite the improvement, issues still persist and have given rise to alternative transpilers (v8) and languages(TypeScript). Below are 5 odds things about JavaScript
1. Adding A Number To A String
JavaScript can handle adding a number to a string, but the result might not be what you expect. When you add a number and a string, JavaScript converts the number to a string and concatenates them instead of performing mathematical addition. For example, 1 + "2"
results in the string "12"
, not the number 3
.
Also, subtracting a number from a string. when you run "2" - 1
, it then takes a mathematical approach, instead of using the previous behavior. It converts the string "2" to a number and makes the subtraction, resulting to 1
.
In other languages like Python and Java. running code like this will automatically through you an error. error: you can't concatenate a string to a number.
2. Using The typof
Keyword on an Array
If you use the typeof
keyword on an array in JavaScript, it will return "object"
, not "array"
. This can be surprising, as you might expect a more specific type identifier. To check if a variable is an array, you can use additional methods like Array.isArray()
.
Also to check the typeof
of "not a number (Nan
)" returns number. there is definitely something confusing with the javascript typeof keyword.
3. Creating an Object Variable With A const
Keyword
As you might already know, creating a variable with the const
keyword means the variable can't be changed. But while creating an Object variable with the const keyword, you can still change its property.๐
const car = {
brand: "Toyota",
color: "red"
}
//property can still be changed, even with the const keyword
car.color = "red";
While you can create an object using the const
keyword in JavaScript, it doesn't make the object itself immutable or unchangeable. The const
keyword only means that the reference to the object can't be changed, but you can still modify the object's properties.
4. 0.1 + 0.2 != 0.3
checking if 0.1 + 0.2 === 0.3
returns False. Apparently, 0.1 + 0.2
in javascript isn't 0.3
, but 0.30000000000000004
.๐ด
This is a classic floating-point precision issue. Due to the way floating-point numbers are represented in JavaScript, the result of 0.1 + 0.2
is not exactly 0.3
. It's a tiny bit off due to the internal representation of these numbers, leading to unexpected results when precision is crucial. Which is just a complex way of saying, we can't do math ๐งฎ.
5. Comparing Empty Arrays
Here's a bizarre quirk - comparing two empty arrays
return false
[] === []; // -> False
This odd behavior can be perplexing and seems quite counterintuitive. It's one of those quirky aspects of JavaScript that can leave developers scratching their heads.
Conclusion
Despite its unique characteristics and criticisms, JavaScript remains a versatile and impactful language. It adapts by adding new features, tools, and through collaborative efforts from the programming community, all aimed at addressing its limitations as time goes on.
Subscribe to my newsletter
Read articles from Winning Godspower directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by