Python.v.JavaScript

JavaScript for Pythonistas. Python for JavaScripters

Types and comparison

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 #
a = '32'
b = 7
a + b
Exception: TypeError
can only concatenate str (not "int") to str
a * b
'32323232323232'
a = '32'
b = 7a + b;
327
a - b;
25
a * b;
224
a / b;
4.571428571428571
List /array to string coercion #
first_list = [1, 2, 10]
second_list = [5, 3]
first_list + second_list
[1, 2, 10, 5, 3]
var first_array = [1, 2, 10];
var second_array = [5, 3];first_array + second_array;
1,2,105,3

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).


var first_array = [1, 2, 10];
var second_array = [5, 3];first_array.concat(second_array);
[ 1, 2, 10, 5, 3 ]
Comparison and coercion #
a = '12'
b = 12
if a == b:
    print("a is equal to b")
else:
    print("a is not equal to b")
if str(a) == str(b):
    print("str(a) is equal to str(b)")
if int(a) == int(b):
    print("int(a) is equal to int(b)")
a is not equal to b
str(a) is equal to str(b)
int(a) is equal to int(b)
var a = '12';
var b = 12;
if (a == b) {
    console.log("a is equal to b",
                "(coercions allowed)");
}
if (a === b) {
    console.log("a is strictly equal to b", 
                "(coercions are not allowed)");
}
if (Number(a) === Number(b)) {
    console.log("Number(a) is equal", 
                "to Number(b)");
}
if (String(a) === String(b)) {
    console.log("String(a) is equal", 
                "to String(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 #
[] == 0
False
[] == False
False
"" == []
False
"" == 0
False
[] == 0;
true
[] == false;
true
"" == [];
true
"" == 0;
true
List / array comparison #
first_list = [1, 2, 3]
second_list = [1, 2, 3]
third_list = first_list
first_list == second_list
True
first_list == third_list
True
first_list is second_list
False
first_list is third_list
True

Lists are compared elementwise. If you want to check that two variables point to the same list, you have to use operator is.

var first_array = [1, 2, 3];
var second_array = [1, 2, 3];
var third_array = first_array;first_array == second_array;
false
first_array === second_array;
false
first_array == third_array;
true
first_array === third_array;
true
first_array == "1,2,3";
true
first_array === "1,2,3";
false

If both arguments of comparison operator (either == or ===) are arrays, true is returned only if both arguments point to the same object. No elementwise comparison occures. If one of the arguments is string and another is object, and == operator is used, then object is coerced to string and they are compared as strings. If === is used and arguments have different types (string and array), then false is returned. You can find some recipes on how to compare two arrays elementwise here.

Numeric types #
a = 2
b = 2.0
type(a)
<class 'int'>
type(b)
<class 'float'>
a == b
True
type(a) == type(b)
False
my_list = [0, 10, 20, 30]
my_list[b]
Exception: TypeError
list indices must be integers or slices, not float

Python has integer (int) and floating point (float) numbers. In some context, only one of this types is acceptable (i.e. index of list has to be integer number).

var a = 2;
var b = 2.0;typeof(a);
number
typeof(b);
number
a === b;
true
typeof(a) === typeof(b);
true
var my_array = [0, 10, 20, 30]my_array[b];
20

In JavaScript, there's no distinction between integers and floating point numbers. In fact, all numbers are floating points.

© Ilya V. Schurov and contributors, 2017
Licenses: CC BY (text), MIT (code).
contribute on github