Python and Javascript are both languages with dynamical types. Variables do not have types but the values have. Type transformation (explicit and implicit) is a common source of surprises (especially if you are switching from Python to JavaScript).
Python | Javascript |
---|---|
Number and string coercion # | |
Exception: TypeError can only concatenate str (not "int") to str
'32323232323232' |
327
25
224
4.571428571428571 |
List /array to string coercion # | |
[1, 2, 10, 5, 3] |
1,2,105,3
[ 1, 2, 10, 5, 3 ] |
Comparison and coercion # | |
a is not equal to b str(a) is equal to str(b) int(a) is equal to int(b) |
a is equal to b (coercions allowed) Number(a) is equal to Number(b) String(a) is equal to String(b) |
Edge cases in comparison # | |
False
False
False
False |
true
true
true
true |
List / array comparison # | |
True
True
False
True Lists are compared elementwise. If you want to check that two
variables point to the same list, you have to use operator |
false
false
true
true
true
false If both arguments of comparison operator (either |
Numeric types # | |
<class 'int'>
<class 'float'>
True
False
Exception: TypeError list indices must be integers or slices, not float Python has integer ( |
number
number
true
true
20 In JavaScript, there's no distinction between integers and floating point numbers. In fact, all numbers are floating points. |
Ooops. What's that? If operator
+
cannot make an addition (i.e. its arguments are not numbers), it coerces all the arguments to strings and then concatenate it (as strings).