Skip to content

Language Syntax Comparison

Quick reference for common syntax patterns across C#, JavaScript/Node.js, and Python. Useful when switching between languages or working in polyglot environments.

String Interpolation

String interpolation allows embedding variables and expressions directly in strings.

LanguageSyntaxExample
C#$"text {variable}"$"my name is {name}"
JavaScript/Node`text ${variable}``my name is ${name}`
Python 3.6+f"text {variable}"f"my name is {name}"
Python (older)"text {}".format(variable)"my name is {}".format(name)

Examples

C#:

csharp
string name = "John";
int age = 30;

// Basic interpolation
string message = $"my name is {name}";

// With expressions
string details = $"{name} is {age} years old";

// Multi-line (C# 11+)
string multiline = $"""
    Name: {name}
    Age: {age}
    """;

JavaScript/Node.js:

javascript
const name = "John";
const age = 30;

// Basic interpolation
const message = `my name is ${name}`;

// With expressions
const details = `${name} is ${age} years old`;

// Multi-line
const multiline = `
    Name: ${name}
    Age: ${age}
`;

Python:

python
name = "John"
age = 30

# f-string (Python 3.6+, recommended)
message = f"my name is {name}"

# With expressions
details = f"{name} is {age} years old"

# format() method (older style)
message_old = "my name is {}".format(name)

# Multi-line
multiline = f"""
    Name: {name}
    Age: {age}
"""

Variable Declaration

LanguageSyntaxNotes
C#int x = 5;
var x = 5;
Statically typed, var infers type
JavaScriptlet x = 5;
const x = 5;
let is mutable, const is immutable
Pythonx = 5Dynamically typed, no keywords needed

Function Definition

C#:

csharp
// Named function
public int Add(int a, int b)
{
    return a + b;
}

// Lambda
Func<int, int, int> add = (a, b) => a + b;

JavaScript:

javascript
// Named function
function add(a, b) {
    return a + b;
}

// Arrow function
const add = (a, b) => a + b;

Python:

python
# Named function
def add(a, b):
    return a + b

# Lambda
add = lambda a, b: a + b

Conditionals

C#:

csharp
if (x > 10)
{
    Console.WriteLine("Greater");
}
else if (x == 10)
{
    Console.WriteLine("Equal");
}
else
{
    Console.WriteLine("Less");
}

// Ternary
string result = x > 10 ? "Greater" : "Less or equal";

JavaScript:

javascript
if (x > 10) {
    console.log("Greater");
} else if (x === 10) {
    console.log("Equal");
} else {
    console.log("Less");
}

// Ternary
const result = x > 10 ? "Greater" : "Less or equal";

Python:

python
if x > 10:
    print("Greater")
elif x == 10:
    print("Equal")
else:
    print("Less")

# Ternary
result = "Greater" if x > 10 else "Less or equal"

Loops

For Loop

C#:

csharp
// Traditional for loop
for (int i = 0; i < 10; i++)
{
    Console.WriteLine(i);
}

// Foreach
foreach (var item in items)
{
    Console.WriteLine(item);
}

JavaScript:

javascript
// Traditional for loop
for (let i = 0; i < 10; i++) {
    console.log(i);
}

// For-of (for arrays)
for (const item of items) {
    console.log(item);
}

// forEach method
items.forEach(item => console.log(item));

Python:

python
# Range-based for loop
for i in range(10):
    print(i)

# For-in (for iterables)
for item in items:
    print(item)

While Loop

C#:

csharp
while (condition)
{
    // code
}

JavaScript:

javascript
while (condition) {
    // code
}

Python:

python
while condition:
    # code

Collections

Arrays/Lists

C#:

csharp
// Array
int[] numbers = { 1, 2, 3, 4, 5 };

// List
var list = new List<int> { 1, 2, 3, 4, 5 };

// Access
int first = numbers[0];

JavaScript:

javascript
// Array
const numbers = [1, 2, 3, 4, 5];

// Access
const first = numbers[0];

// Methods
numbers.push(6);
numbers.pop();

Python:

python
# List
numbers = [1, 2, 3, 4, 5]

# Access
first = numbers[0]

# Methods
numbers.append(6)
numbers.pop()

Dictionaries/Objects/Maps

C#:

csharp
// Dictionary
var dict = new Dictionary<string, int>
{
    ["one"] = 1,
    ["two"] = 2
};

// Access
int value = dict["one"];

JavaScript:

javascript
// Object
const obj = {
    one: 1,
    two: 2
};

// Map
const map = new Map([
    ["one", 1],
    ["two", 2]
]);

// Access
const value = obj.one;  // or obj["one"]
const mapValue = map.get("one");

Python:

python
# Dictionary
d = {
    "one": 1,
    "two": 2
}

# Access
value = d["one"]  # or d.get("one")

Class Definition

C#:

csharp
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    public void Greet()
    {
        Console.WriteLine($"Hello, I'm {Name}");
    }
}

JavaScript:

javascript
class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    greet() {
        console.log(`Hello, I'm ${this.name}`);
    }
}

Python:

python
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        print(f"Hello, I'm {self.name}")

Error Handling

C#:

csharp
try
{
    // code that might throw
}
catch (Exception ex)
{
    // handle error
    Console.WriteLine(ex.Message);
}
finally
{
    // cleanup
}

JavaScript:

javascript
try {
    // code that might throw
} catch (error) {
    // handle error
    console.log(error.message);
} finally {
    // cleanup
}

Python:

python
try:
    # code that might raise
except Exception as e:
    # handle error
    print(str(e))
finally:
    # cleanup

Null/None Handling

C#:

csharp
// Null check
if (value != null)
{
    // use value
}

// Null-coalescing
string result = value ?? "default";

// Null-conditional
int? length = value?.Length;

JavaScript:

javascript
// Null/undefined check
if (value != null) {
    // use value
}

// Nullish coalescing
const result = value ?? "default";

// Optional chaining
const length = value?.length;

Python:

python
# None check
if value is not None:
    # use value

# Default value
result = value if value is not None else "default"

# or using 'or' (but beware of falsy values)
result = value or "default"

See Also

Released under the MIT License.