console.log(“x is greater than 10”);
In this example, the condition being checked is whether the variable x is greater than 10. If this condition is true, the console will output “x is greater than 10.”
When a user submits a form on a website, you may need to check that they have entered valid data. For example, you might check that they have entered a valid email address or that they have filled in all required fields.
If you have a login system on your website, you will need to check that the user has entered valid login credentials before allowing them to access protected content.
In some cases, you may want to display different content or functionality based on certain conditions. For example, you might display a different menu for logged in users versus non-logged in users.
The ternary operator allows you to write a more concise version of an if statement. It works like this:
In this example, the ternary operator checks whether x is greater than 10. If it is, the message variable is set to “x is greater than 10,” otherwise it is set to “x is less than or equal to 10.”
console.log(“Invalid day of week”);
In this example, the switch statement checks the value of the dayOfWeek variable and outputs the corresponding day of the week.
Truthy and Falsy Values
console.log(“x is truthy”);
In this example, the if statement checks whether x is truthy. Since x is set to 10, which is truthy, the console outputs “x is truthy.”