0% found this document useful (0 votes)
1 views

Programming concepts class3

The document compares conditionals in JavaScript and Python, highlighting differences in syntax and structure. Key distinctions include the use of parentheses and curly braces in JavaScript versus indentation in Python, as well as the naming of `else if` and `switch` versus `elif` and `match`. Additionally, it outlines how both languages handle truthy and falsy values differently.

Uploaded by

Federico Czyrka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Programming concepts class3

The document compares conditionals in JavaScript and Python, highlighting differences in syntax and structure. Key distinctions include the use of parentheses and curly braces in JavaScript versus indentation in Python, as well as the naming of `else if` and `switch` versus `elif` and `match`. Additionally, it outlines how both languages handle truthy and falsy values differently.

Uploaded by

Federico Czyrka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Great question!

JavaScript and Python both use conditionals to control the flow of


execution, but they have some syntactic and structural differences. Here�s a
comparison:

### **Basic `if` Statements**


Both JavaScript and Python use `if` statements in a similar way.

#### **JavaScript**
```javascript
let temperature = 20;
if (temperature > 25) {
console.log("It's a hot day!");
}
```
#### **Python**
```python
temperature = 20
if temperature > 25:
print("It's a hot day!")
```
- **Key Difference**: Python does not require parentheses around the condition and
uses indentation instead of curly braces `{}` to define blocks.

---

### **`if-else` Statements**


Both languages support `if-else` structures for handling multiple possible
outcomes.

#### **JavaScript**
```javascript
let age = 18;
if (age >= 21) {
console.log("You can drink in the US.");
} else {
console.log("You're not old enough.");
}
```
#### **Python**
```python
age = 18
if age >= 21:
print("You can drink in the US.")
else:
print("You're not old enough.")
```
- **Key Difference**: Python eliminates `{}` and uses indentation for defining
blocks.

---

### **`else if` vs `elif`**


JavaScript uses `else if`, while Python uses `elif`.

#### **JavaScript**
```javascript
let score = 75;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else if (score >= 70) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}
```
#### **Python**
```python
score = 75
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")
```
- **Key Difference**: Python uses `elif` instead of `else if`, making it slightly
more concise.

---

### **`switch` Statements vs `match`**


JavaScript has `switch` statements, whereas Python recently introduced `match`
statements (Python 3.10+).

#### **JavaScript (`switch`)**


```javascript
let day = "Monday";
switch (day) {
case "Monday":
console.log("Start of the week!");
break;
case "Friday":
console.log("Almost the weekend!");
break;
default:
console.log("Just another day.");
}
```
#### **Python (`match`)**
```python
day = "Monday"
match day:
case "Monday":
print("Start of the week!")
case "Friday":
print("Almost the weekend!")
case _:
print("Just another day.")
```
- **Key Difference**: Python�s `match` is more structured and avoids using `break`
statements.

---
### **Truthy & Falsy Values**
- JavaScript considers non-boolean values like `0`, `""`, `null`, and `undefined`
as **falsy**.
- Python treats `None`, `0`, `""`, and empty collections like `[]`, `{}` as
**falsy**.

#### **JavaScript Example**


```javascript
let value = "";
if (value) {
console.log("Truthy!");
} else {
console.log("Falsy!");
} // Output: "Falsy!"
```
#### **Python Example**
```python
value = ""
if value:
print("Truthy!")
else:
print("Falsy!") # Output: "Falsy!"
```

---

### **Summary of Differences**


| Feature | JavaScript | Python |
|---------|-----------|--------|
| `else if` | Uses `else if` | Uses `elif` |
| `switch` | Uses `switch` with `case` | Uses `match` (Python 3.10+) |
| Block Definition | `{}` for code blocks | Indentation for code blocks |
| Truthy & Falsy Values | `"0", "", null, undefined` | `"0", "", None, [], {}` |

Would you like an example that applies to a project you're working on? Since you're
skilled in JavaScript and Python, I can tailor the explanation to a real-world
scenario you're tackling!

You might also like