DEV Community

Maksym
Maksym

Posted on

Python vs JavaScript Keywords Comparison

Overview

Python has 35 keywords while JavaScript has 63+ reserved words (including future reserved words). Both languages use keywords to define syntax and control program flow, but they differ significantly in approach and philosophy.

Control Flow Comparison

Conditionals

Python JavaScript Notes
if if Same concept
elif else if Python uses single keyword, JS uses two words
else else Same

Python Example:

if age >= 18:
    print("Adult")
elif age >= 13:
    print("Teen") 
else:
    print("Child")
Enter fullscreen mode Exit fullscreen mode

JavaScript Example:

if (age >= 18) {
    console.log("Adult");
} else if (age >= 13) {
    console.log("Teen");
} else {
    console.log("Child");
}
Enter fullscreen mode Exit fullscreen mode

Loops

Python JavaScript Notes
for for Different syntax, Python more intuitive
while while Similar concept
break break Same
continue continue Same
- do JS has do-while, Python doesn't

Python for loop:

for item in items:
    print(item)

for i in range(10):
    if i == 5:
        continue
    if i == 8:
        break
Enter fullscreen mode Exit fullscreen mode

JavaScript for loop:

for (let item of items) {
    console.log(item);
}

for (let i = 0; i < 10; i++) {
    if (i === 5) continue;
    if (i === 8) break;
}
Enter fullscreen mode Exit fullscreen mode

Functions & Classes

Python JavaScript Notes
def function Python shorter
class class Same (ES6+)
return return Same
yield yield Same (generators)
lambda Arrow functions => Different syntax

Python:

def greet(name):
    return f"Hello, {name}"

# Lambda
square = lambda x: x ** 2

class Person:
    def __init__(self, name):
        self.name = name
Enter fullscreen mode Exit fullscreen mode

JavaScript:

function greet(name) {
    return `Hello, ${name}`;
}

// Arrow function
const square = x => x ** 2;

class Person {
    constructor(name) {
        this.name = name;
    }
}
Enter fullscreen mode Exit fullscreen mode

Variable Scope

Python JavaScript Notes
global - Python explicit global declaration
nonlocal - Python closure variable modification
- var JS function-scoped (legacy)
- let JS block-scoped
- const JS constant declaration

Python Scope:

x = 10  # global

def outer():
    x = 20  # local to outer

    def inner():
        global x  # refers to global x
        x = 30

        nonlocal x  # ERROR: can't use both
        # nonlocal would refer to outer's x
Enter fullscreen mode Exit fullscreen mode

JavaScript Scope:

var x = 10;    // function-scoped
let y = 20;    // block-scoped  
const z = 30;  // constant

function example() {
    var a = 1;   // function-scoped
    let b = 2;   // block-scoped
    const c = 3; // constant

    if (true) {
        var a = 4;   // same 'a' as above
        let b = 5;   // different 'b'
        // const c = 6; // would shadow outer 'c'
    }
}
Enter fullscreen mode Exit fullscreen mode

Exception Handling

Python JavaScript Notes
try try Same
except catch Different names
finally finally Same
raise throw Different names

Python:

try:
    result = 10 / 0
except ZeroDivisionError as e:
    print(f"Error: {e}")
except Exception as e:
    print(f"General error: {e}")
finally:
    print("Cleanup")

raise ValueError("Custom error")
Enter fullscreen mode Exit fullscreen mode

JavaScript:

try {
    let result = 10 / 0;  // Returns Infinity, doesn't throw
    throw new Error("Custom error");
} catch (error) {
    console.log(`Error: ${error.message}`);
} finally {
    console.log("Cleanup");
}
Enter fullscreen mode Exit fullscreen mode

Logical Operators

Python JavaScript Notes
and && Python uses words
or `\ \
{% raw %}not ! Python uses word
in in Same for objects/arrays
is === Identity vs strict equality

Python:

if age >= 18 and has_license:
    can_drive = True

if name in users or is_admin:
    allow_access = True

if user is None:
    return "No user"
Enter fullscreen mode Exit fullscreen mode

JavaScript:

if (age >= 18 && hasLicense) {
    canDrive = true;
}

if (name in users || isAdmin) {
    allowAccess = true;
}

if (user === null) {
    return "No user";
}
Enter fullscreen mode Exit fullscreen mode

Unique to Each Language

Python-Only Keywords

  • elif - more concise than "else if"
  • pass - explicit no-operation
  • del - explicit object deletion
  • with - context managers
  • assert - built-in debugging
  • nonlocal - closure variable modification

JavaScript-Only Keywords

  • var, let, const - different variable declarations
  • switch, case, default - switch statements
  • do - do-while loops
  • new - object instantiation
  • this - context reference
  • typeof, instanceof - type checking
  • void - undefined operator
  • debugger - breakpoint statement

Async Programming

Python JavaScript Notes
async async Same (Python 3.5+)
await await Same

Python:

import asyncio

async def fetch_data():
    await asyncio.sleep(1)
    return "data"

async def main():
    result = await fetch_data()
    print(result)
Enter fullscreen mode Exit fullscreen mode

JavaScript:

async function fetchData() {
    await new Promise(resolve => setTimeout(resolve, 1000));
    return "data";
}

async function main() {
    const result = await fetchData();
    console.log(result);
}
Enter fullscreen mode Exit fullscreen mode

Key Philosophical Differences

Python Philosophy

  • Explicit is better than implicit - keywords like global, nonlocal
  • Readability counts - and, or, not instead of symbols
  • One way to do it - fewer keywords, more consistent

JavaScript Philosophy

  • Flexibility - multiple ways to declare variables (var, let, const)
  • C-style syntax - symbols over words (&&, ||, !)
  • Backward compatibility - keeps old keywords even when better alternatives exist

Modern Trends

Both languages are converging in some areas:

  • Both added async/await for asynchronous programming
  • Both use class keyword for OOP
  • Both support similar control flow patterns

However, Python remains more consistent and readable, while JavaScript offers more flexibility at the cost of complexity.

Feel free to express your thoughts and criticize this article!

Top comments (2)

Collapse
 
sami_ammar_fb38e674b4a49e profile image
Sami Ammar

And an other thanks for this wonderfull project.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.