Build in-demand skills in Northwestern’s online MS in Information Design

Build in-demand skills in Northwestern’s online MS in Information Design

blog.alexdevero.com blog.alexdevero.com2 weeks ago in #Dev Love67

JavaScript if else statement makes it easy to execute code based on different conditions. This tutorial will help you learn all you need to know about if else statement. You will learn how to use if, else, else if and nested if else. You will also learn about ternary operator and much more. The if statement JavaScript if else statement makes it very easy to execute your code if specific conditions is truthy. Its syntax is just as easy. It is composed of three parts. The first part is if keyword. You use this keyword to tell JavaScript that you are about to create an if else statement. The second part is a condition you want to test for. Condition is wrapped with parentheses and it follows the if keyword. This can condition can range from very simple to very complex expressions. The only thing that matters is if the result of that expression is boolean, either true or false. The third part is a block of code you want to execute. This block of code is surrounded by curly brackets. It follows right after the condition. Remember that this block of code will be executed only if the condition evaluates to true, is truthy. If it evaluates to false, it is falsy, the block of code will not be executed. // If else statement syntax if (condition) { // block of code to execute // if condition is truthy } // Example of if statement: truthy condition // Create a variable and assign it a number const num = 10 // Create an if statement that tests // if the value of num variable is bigger than 5 // this is the condition if (num 5) { // If num is bigger than 5 run the code below console.log(‘The value of num is bigger than 5.’) } // Output: // ‘The value of num is bigger than 5.’ // Example of if statement: falsy condition // Create a variable and assign it a string const name = ‘Rick’ // Create an if statement that tests // if the value of name variable starts with ‘A’ // this is the condition if (name[0] === ‘A’) { // If the value of name starts with ‘A’ run the code below console.log(‘The value of name starts with ‘A’.’) } // Output: // … nothing Negating the condition There is one thing about if statement, and the condition, worth mentioning. You can quickly make any condition truthy or falsy, by using logical NOT operator (!). This logical operator will negate any boolean expression. It will transform true to false and false to true. // Example of if statement: using logical NOT operator // Create a variable and assign it a number const num = 10 // Create an if statement that tests // if the value of num variable is NOT bigger than 5 if (!num 5) { // = 50) { // If num is bigger than or equal to 50 run the code below console.log(‘The value of num is bigger than 50.’) } else { // If num is smaller than 50 run the code below console.log(‘The value of num is bigger than 50.’) } // Output: // ‘The value of num is bigger than 50.’ One thing about the else statement. This part of if else statement is purely optional. You don’t have to use it if you don’t want to, or if there is no reason to do so. The else if You know how to use if else statement to execute one snippet of code when condition is…

Like to keep reading?

This article first appeared on blog.alexdevero.com. If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply