Syntax
Comparing the syntax differences between Python and JavaScript
| Feature | JavaScript | Python |
|---|---|---|
| Variable Declaration |
let x = 10;
|
x = 10
|
| Comments |
|
|
| Boolean | let flag = true; |
flag = True (case-sensitive) |
| Types | typeof(x) |
type(x)
|
| Type Coercion |
JavaScript automatically performs type coercion.
|
Python requires explicit conversion for most types.
|
| String Literals |
|
|
| Slice |
|
|
| Upper case | console.log("hello".toUpperCase());// 'HELLO' |
print("hello".upper())# 'HELLO' |
| Replace | console.log("hello".replace("l", "L"));// 'heLlo' replaces first ONLY the first occurrence |
print("hello".replace("l", "L"))# 'heLLo' replaces all occurrencesprint("hello".replace("l", "L", 1))# 'heLLo' replaces only the first occurrence
|
| Includes | console.log("hello".includes("e"));// true |
print("e" in "hello") # True (includes -> in)
|
| === | console.log([]===[]) // true |
print([]==[]) # True |
| ! | console.log(!true) // false |
print(not True) # False |
| Ternary | console.log(true ? "yes" : "no") // "yes"const age = 20; const status = age >= 18 ? "adult" : "minor";
|
print("yes" if True else "no") # "yes"age = 20;"adult" if age >= 18 else "minor" |
| && || => and or | console.log( 2 < 5 && 7 < 5) // false console.log( 2 < 5 || 7 < 5) // true |
print( 2 < 5 and 7 < 5) # Falseprint( 2 < 5 or 7 < 5) # True |
| Destructuring/ unpacking | const [first, second, third] = [1, 2, 3]; |
first, second, third = [1, 2, 3] |
| spread operator | const [head, ...tail] = [1, 2, 3, 4, 5]; // ... spread; |
head, *tail = [1, 2, 3, 4, 5] |
| Loops | for (let i = 0; i < 5; i++) {} |
for i in range(5): |