JavaScript Conditionals & Logical Operators

JavaScript Conditionals & Logical Operators

What are conditionals?

A conditional regulates behavior and decides whether or not certain bits of code can be executed.

Types of conditionals

Conditionals.png

Examples

if statements

if (12 < 5) {
console.log(true);
}

else statements

if (12 < 5) {
console.log(true);
} else {
console.log(false);
}

else if statements

if (12 < 5) {
console.log(true);
} else if (12 > 5) {
console.log(false);
} else {
console.log(null);
}

Conditional operators

Conditional operators.png

What are logical operators?

Logical statements employ logical operators to assess the equality or disparity of variables or values.

Types of logical operators

Logical operators (2).png

Examples

and statements

(x < 10 && y > 1)

or statements

(x == 5 || y == 5)

not statements

!(x == y)

Summary

A conditional regulates behavior and decides whether or not certain bits of code can be executed. There are three types of conditionals: if, else and else if. There are different conditional operators used in JavaScript, too.

A logical operator use logical statements to assess the equality of variables or values. There are 3 logical operators in Javascript: and, or an not.

At first, these may be a little complicatedto master, but with practice, you will have a better understanding of it.

References

w3schools.com