Rules of programming languages can not change, the whole world depends on them stable, would break everything that people did.
Well, in fact they even change when there was a serious error in the language, or we’re talking about PHP that doesn’t mind breaking a lot of stuff and letting everyone else in. But I don’t even know if this is bad because they improve several mistakes that the language had. Of course, there’s a very bad side.
Javascript is a language full of bad rules. Almost all language problems are related to poorly defined rules.
Dynamic typing against the DOM
In the specific case there are errors because of weak typing which is something a language should not have, this causes several problems, and there is error because of the poorly done implementation of the DOM in the browser.
Let’s change the code to see what’s going on:
var name = "10kg" / 10;
console.log(typeof name);
if (name) console.log(name);
else console.log("Not exist");
I put in the Github for future reference.
Note that the variable name
is the type string
. So since a number is kind of string
? Unlike the pattern we learned that JS has dynamic typing, DOM object variables are not dynamic typing and if you put a value of a different type than you expect there yes the language implicitly converts the value to the variable type.
I put in the Github for future reference.
Weak typing
Then there is another weak typing error. When a text can be completely converted to number it is, and it should not happen. But when you have a part that is text, the conversion does not occur, and then instead of generating a valid value generates an Nan.
Then comes another weak typing error. When using a value in a place that expects a boolean it uses the automatic conversion rule to get the expected value, and a string with any text is true. A rule is very clear on that, just one string empty is false (which is another weird rule). There is a text, it is not empty. This should be prohibited in any decent language, but it is not in JS.
Three automatic conversions that should not have been made caused confusion. And in general people do not understand or do not know about them. Just because it’s not intuitive shouldn’t be allowed.
What you have to do?
- Never use a value that is not specifically of the type you want. Do not split with a string and do not use a value that is not a boolean where one expects a value like
if
for example. If you always remember this JS will be a much better experience for you.
- Never use DOM variable names unless they are treated as local. I already explain
Done like this you don’t even need the if
because it deals with a programming error, and programming errors must be fixed and not checked running, can not be treated as something valid, even if the language allows.
Although this case didn’t even work with the expected outcome.
Works perfectly (but is not yet certain):
var name = 10 / 10;
if (name) console.log(name);
else console.log("Not exist");
I put in the Github for future reference.
Even better to create a boolean the right way:
var name = 10 / 10;
if (name != 0) console.log(name);
else console.log("Not exist");
I put in the Github for future reference.
Inappropriate variable name
A numeric variable called name
creates confusion (even if it were nome
because names are not numbers). And then we see the Javascript problem (in the browser). Let’s change the name:
var x = "10kg" / 10;
console.log(typeof x);
if (x) console.log(x);
else console.log("Not exist");
I put in the Github for future reference.
Note that changing the variable name is another behavior. The conversion is not done and happens as expected. In fact it has a conversion, but it is done after.
name
is not actually a local variable, it is the DOM variable whose full name is window.name
and you can’t have your type changed.
You can use the name
ensure that you are using a local variable:
let name = "10kg" / 10;
console.log(typeof name);
if (name) console.log(name);
else console.log("Not exist");
I put in the Github for future reference.
So in Node/Deno it can happen differently (see comment above), which is another crazy of the entire ecosystem. But nothing strange because all web technology was poorly thought out.
Completion
And that’s one more reason I can say that dynamic typing is wrong in most scenarios, it’s not just weak typing that’s bad at all. Even in scripts which I thought was right, is wrong because they are often used embedded in other applications that do not have dynamic typing and creates an impedance, and then all of this happens.
Javascript should not allow automated conversions that cause confusion. Even in the case of DOM that cannot have dynamic typing, the solution should be to give an error of execution when trying to change the type of the variable and not make a senseless conversion. Violates the typing style, but does not generate a false result.
The curious thing is that if you test this code in Nodejs this code returns
Not exist
and if you test it on Deno it does not compile with errorerror: TS2362 [ERROR]: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
let name = "10kg" / 10;
with the"10kg"
unbridled.– Augusto Vasques
This is really with the colleague explained, they are traps, and the beginners (here tbm include me), fall into it and have no idea what happened. Very strange.
– Alberty Lucas