Conditionals

As a Python developer, you're already familiar with conditional statements – blocks of code that run based on whether certain conditions are true or false. The concept in JavaScript remains the same, but with a few key differences that we'll discuss in this article.

The if Statement

The if statement in JavaScript functions similarly to Python. Here's a side-by-side comparison:

Python:

x = 10
if x > 5:
    print("x is greater than 5")

JavaScript:

let x = 10;
if (x > 5) {
    console.log("x is greater than 5");
}

The syntax in JavaScript requires parentheses around the condition, unlike Python. Also, you'll notice that JavaScript uses curly braces {} to denote the block of code associated with the condition, and the line ends with a semicolon.

else and else if

Just like Python, JavaScript has else and else if clauses. These work very similarly to their Python counterparts.

Python:

x = 10
if x > 10:
    print("x is greater than 10")
elif x == 10:
    print("x is exactly 10")
else:
    print("x is less than 10")

JavaScript:

let x = 10;
if (x > 10) {
    console.log("x is greater than 10");
} else if (x === 10) {
    console.log("x is exactly 10");
} else {
    console.log("x is less than 10");
}

Note that elif in Python is equivalent to else if in JavaScript. Also, == in JavaScript performs type coercion and checks for equality of value, but not type. The === operator checks for equality of both value and type.

Switch Case

JavaScript has a switch statement, which is used to perform different actions based on different conditions. Python doesn't have a built-in switch case statement, and it is often simulated using if..elif..else chains or dictionaries.

Here's a simple example of a switch statement in JavaScript:

let fruit = "Apple";

switch(fruit) {
    case "Apple":
        console.log("Red");
        break;
    case "Banana":
        console.log("Yellow");
        break;
    default:
        console.log("Unknown fruit");
}

The switch statement evaluates an expression and checks it against different case values. If there's a match, the corresponding block of code is executed. The break keyword is used to exit the switch case. Without it, the program will continue to the next case, causing potentially unwanted behavior. The default keyword specifies code to run if there's no match.

Truthy and Falsy Values

In JavaScript, all values have an inherent boolean truthiness or falsiness. This differs from Python, which has a specific set of rules for what evaluates to True or False.

In JavaScript, the falsy values are false, 0, "" (empty string), null, undefined, and NaN. Everything else is truthy.

This is important to understand because it impacts how conditions are evaluated in JavaScript. For example:

let name = "";
if (name) {
    console.log("Hello, " + name);
} else {
    console.log("No name provided");
}

In this example, if name is an empty string, the message "No name provided" will be logged to the console.

Ternary Operator

JavaScript, like Python, has a ternary operator for handling simple conditional logic in a concise way. Here's a comparison:

Python:

x = 10
message = "x is 10" if x == 10 else "x is not 10"

JavaScript:

let x = 10;
let message = x === 10 ? "x is 10" : "x is not 10";

The syntax is slightly different, but the concept is the same: evaluate a condition and return one value if it's true, and another value if it's false.