Python vs JS Cheatsheet
This cheat-sheet is handy for all of you who throttle between python and javascript
None
None # python
null // javascript
else if
if val == 0: # python
pass
elif val == 1:
pass
else
pass
if (val == 0) { // javascript
}
else if (val == 1) {
}
else {
}
Type testing
if isinstance(test, bool) // python
if (typeof test === 'boolean') // javascript
Type conversions
some of them
st = str(i) // python
let st = i.toString() // javascript
i = int(st) // python
let i = parseInt(st) // javascript
Strings
append
st = st + 'more' # python
st = st.concat('more') // javascript
length
len(st) # python
st.length // javascript
interpolation
'kikkelis-kokkelis-{parameter}'.format(parameter=1) # python
f'kikkelis-kokkelis-{parameter} # python
`kikkelis-kokkelis-${parameter}" // javascript
splitting
str.split(",") // python AND javascript
substring check
substring in string # python
string.includes(substring) // javascript
Lists, Arrays
iteration
for item in lis: # python
<code>
list.forEach(item => { // javascript
<code> // NOTE: if you need early termination, don't use forEach
})
for (const item of lis) { // javascript
<code>
}
for i, item in enumerate(lis): # python
<code>
for (var i = 0; i < list.length; i++) { // javascript
var item = list[i]
<code>
}
creation
lis = [] # python
lis.append(item)
var lis = [] // javascript
lis.push(item)
modification
lis.pop(index) # python
lis.splice(index, 1) // javascript
find index
lis.index(element) # python
lis.indexOf(element) // javascript
Sets
from a list
s = set(lis) # python
s = Set(lis) // javascript
args
def func(*args): # python
pass # args is a list
function func(...args) { // javascript
// args is an array
}
Dictionaries, Objects
creating
dic = {} # python
dic = Objects() // javascript
access per key
dic[key] # python
dic[key] // javascript
get iterarable / list
dic.keys() # python iterable
Object.keys(dic) // javascript list
iteration
for key, value in dic.items(): # python
<code>
for (const [key, value] of Object.entries(dict)) { // javascript
<code>
}
Object.entries(dict).forEach( // javascript
([key, value]) => {
<code>
}
)
remove key-value
dic.pop("key") # python
dic.delete["key"] // javascript
check for key
'key-name' in dict # python
dict.hasOwnProperty('key-name') // javascript
catch unexisting key
try: # python
value = dict[key]
except KeyError:
<code>
let value = dict[key] // javascript
if (value == undefined) { <code> }
from string
dic = json.loads(st) # python
let dic = JSON.parse(st) // javascript
Deepcopy
a = copy.deepcopy(b) # python
a = structuredClone(b) // javascript
Traceback
import traceback # python
traceback.print_stack()
console.trace() // javascript
Subclassing
call superclass ctor
class ChildClass(ParentClass): # python
def __init__(self):
super()
class ChildClass extends ParentClass { // javascript
constructor() { super() } // javascript
call superclass method
class SubClass(BaseClass): # python
...
def someMethod():
super().someMethod()
class SubClass extends BaseClass { // javascript
...
someMethod() {
super.someMethod(); // NOTE: no `()` in super
}
}
Object Instance Members
self.member = 1 # python
this.member = 1 // javascript
See also this about this.
Lambda Functions
f = lambda x: x+1 # python
(x) => { return x+1 } // javascript
x => x+1 // javascript
JS Scope
let x=1 // only withint current {} scope
const x=1 // only withint current {} scope
var x=1 // { var x=1 {seen also in this scope} }