Day 18 of 21 Days of Javascript
Oh the quirks of JS! I continue to be reminded of why I much prefer Python. Javascript, it turns out, is far more forgiving when it comes to making mistakes with the language (this is why Typescript exists.)
For example let's consider working with strings and numbers.
Python Example
word = "hello"
result = word + 5
# This would give TypeError: can only concatenate str (not "int") to str.
Python requires you to be explicit. So here's what works...
result = word + str(5)
print(result)
# This works fine and prints out: "hello5" after converting 5 to a string.
Javascript Example
let word = "hello";
let result = word + 5;
console.log(result); // This works and you get "hello5"
// JavaScript automatically coerces 5 into string "5", so it concatenates.
Moreover, the below would still run just fine and your program would continue to execute its code without interruption.
let word = "ha";
let result = word * 3;
console.log(result); // Doesn't give error. It returns NaN (Not a Number)
There are other quirks with Javascript, such as the fact that Javascript didn't have a built-in standard for modules until 2015. Developers had to use workarounds just to split code into reusable files and manage dependencies. Meanwhile Python had a import-based module system since 1994!
I still want to get a good understanding of JS despite its weaknesses. But my go-to language will remain Python.