While I’ve got the time, I’m refreshing on the basics of Python before I delve into becoming better at it, and I’ve run into some things I either missed the first time(s) round or that stood out to me as different than JavaScript. The best one this morning was a simple reminder of JavaScript’s type coercion.
Both JavaScript and Python are dynamically typed, meaning you don’t have to declare a type to assign a value to a variable and you can change that variable’s type mid-stream by assigning a different type of value to it.
What is type coercion in JavaScript?
It simply means if you try to add or concatenate two different variable types, JavaScript will usually coerce one to be the same type as the other to make this work. Let’s give a simple example…
let a = 2;
let b = '3';
let c = a + b;
console.log(a, c)
The output is going to be: 2 '23'
This means that the value of a
, which is an integer, was coerced into a string, then 2
and 3
were concatenated like two strings being added together rather than two integers being added together.
The value of a
remains an integer, but it was used as a string for the calculation. This will hold true even if you declare a
with const
instead of let
because the value that’s coerced is a temporary copy, not the variable itself.
That said, you could redefine a
as a string containing “boo” in either language at some point before adding a
and b
together. Neither JavaScript nor Python would throw an error or warn you that the variable’s type had changed. You’d just get 'boo' 'boo3'
as the result.
Meanwhile, in Python, adding 2 to ‘3’ would throw an error before you got to logging the values. But if you had added the line changing 2 to ‘boo’ before the line adding the values, you’d get the same logged values as JavaScript.
Type coercion is dangerous
One of the things that makes dynamically typed languages dangerous is the fact that you can’t always rely on a
to be an integer. The three levels of equal-sign usage in JavaScript are both magnificent and horrifying, depending on your point of view.
a = b; // assigns the value of b to a
a == b; // will use type coercion to compare the two if possible
a === b; // uses strict equality, both must be the same type and value
Those double equals can get tricky.
'1' == 1 // true
'1' === 1 //false
0 == false // true
0 === false // false
An integer can be coerced into a string or a boolean when you’re using two equals. That’s why best practices for JavaScript suggest generally avoiding it and always using three equals.
But Python doesn’t coerce types
If you Google “does python coerce types,” the top answers will all say no. What does that mean?
'1' == 1 // False, because no coercion
'1' + 1 // error because you can't add str and int types with the + operator
Python doesn’t have a triple equals out of the box because it doesn’t need it. As it doesn’t coerce types, it doesn’t need a special comparison mode that prevents coercion.
While '1' == 1
is a true
statement in JavaScript because of type coercion, it’s a False
statement in Python. Also worth noting that I use ‘False’ instead of ‘false’ for Python because it uses a capital letter at the beginning of its boolean values, which threw me the first time I encountered that.
I know, not earthshaking, but something that stood out to me enough to be worth writing about.