Join the DZone community and get the full member experience.Join For Free
what is loose typing?
well, this seems like a good place to start. it is important to understand both what loose typing is , and what loose typing is not . loose typing means that variables are declared without a type. this is in contrast to strongly typed languages that require typed declarations. consider the following examples:
var a = 13; // number declaration
var b = "thirteen"; // string declaration
/* java example (strong typing) */
int a = 13; // int declaration
string b = "thirteen"; // string declaration
ya really - null and undefined too.
type coercion is a topic that is closely associated with loose typing. since data types are managed internally, types are often converted internally as well. understanding the rules of type coercion is extremely important. consider the following expressions, and make sure you understand them:
7 + 7 + 7; // = 21
7 + 7 + "7"; // = 147
"7" + 7 + 7; // = 777
in the examples above, arithmetic is carried out as normal (left to right) until a string is encountered. from that point forward, all entities are converted to a string and then concatenated.
type coercion also occurs when doing comparisons. you can, however, forbid type coercion by using the === operator. consider these examples:
1 == true; // = true
1 === true; // = false
7 == "7"; // = true
7 === "7"; // = false;
there are methods to explicitly convert a variable's type as well, such as parseint and parsefloat (both of which convert a string to a number).
double negation (!!) can also be used to cast a number or string to a boolean. consider the following example:
true == !"0"; // = false
true == !!"0"; // = true
Published at DZone with permission of Jeremy Martin. See the original article here.
Opinions expressed by DZone contributors are their own.
Automating the Migration From JS to TS for the ZK Framework
What Is mTLS? How To Implement It With Istio
13 Impressive Ways To Improve the Developer’s Experience by Using AI
An Overview of Kubernetes Security Projects at KubeCon Europe 2023