JavaScript Fundamentals 02. JS Conditional Statement PDF
Document Details
Uploaded by MagnanimousCloisonnism
Vilnius University
Justina Balsė
Tags
Summary
This document provides notes on JavaScript fundamentals, specifically covering conditional statements such as if-else and switch, logical operators (AND, OR, NOT), and comparison operators. It includes examples and descriptions of how these JavaScript features work.
Full Transcript
JavaScript pagrindai Justina Balsė Turinys Palyginimo operatoriai; boolean tipas; Šakotieji algoritmai (if-else ir switch); Loginiai operatoriai (AND, OR, NOT) 2 Comparison operators | Lyginimo operatoriai...
JavaScript pagrindai Justina Balsė Turinys Palyginimo operatoriai; boolean tipas; Šakotieji algoritmai (if-else ir switch); Loginiai operatoriai (AND, OR, NOT) 2 Comparison operators | Lyginimo operatoriai 3 Boolean: true and false | 1 // true (correct) All comparison operators return a alert( 2 > 1 ); boolean value: // false (wrong) true – means “yes”, “correct” alert( 2 == 1 ); or “the truth”. false – means “no”, “wrong” // true (correct) or “not the truth”. alert( 2 != 1 ); Justina Balsė 4 Boolean: true and false | 2 let myString1 = "abc"; The algorithm to compare two strings is simple: let myString2 = "bcd"; 1. Compare the first character of both strings. 2. If the first character from the first string is greater (or less) than the other string’s, then the first string console.log(myString1 > myString2); is greater (or less) than the second. We’re done. // false 3. Otherwise, if both strings’ first characters are the same, compare the second characters the same way. console.log(myString1 < myString2); 4. Repeat until the end of either string. // true 5. If both strings end at the same length, then they are equal. Otherwise, the longer string is greater. Justina Balsė 5 Do you know what will be the output of this code? console.log(3 > 2 > 1); Justina Balsė 6 IF let year = prompt('In which year The if(...) statement evaluates a was ECMAScript-2015 specification condition in parentheses and, published?', ''); if the result is true, executes a if (year == 2022) { block of code. alert( 'You are right!' ); } Justina Balsė 7 IF...ELSE let year = prompt('In which year was the ECMAScript-2015 specification published?', ''); if (year == 2015) { alert( 'You guessed it right!' ); } else { // any value except 2015 alert( 'How can you be so wrong?' ); } Justina Balsė 8 IF...ELSE IF let year = prompt('In which year was Sometimes, we’d like to test the ECMAScript-2015 specification published?', ''); several variants of a condition. The else if clause lets us do that. if (year < 2015) { alert( 'Too early...' ); } else if (year > 2015) { alert( 'Too late' ); } else { alert( 'Exactly!' ); } Justina Balsė 9 Conditional operator ‘?’ let accessAllowed; // let result = condition ? value1 : value2; let age = prompt('How old are you?', let accessAllowed = (age > 18) ? true : false; ''); if (age > 18) { accessAllowed = true; } else { accessAllowed = false; } alert(accessAllowed); Justina Balsė 10 Logical operators There are three logical operators in JavaScript: || (OR) && (AND) ! (NOT) Justina Balsė 11 OR || let hour = 9; Most of the time, OR || is used in an if statement to test if any of the if (hour < 10 || hour > 18) { alert( 'The office is closed.' ); given conditions is true. } Justina Balsė 12 AND && let hour = 12; AND returns true if both let minute = 30; operands are truthy and false if (hour == 12 && minute == 30) { otherwise. alert( 'The time is 12:30' ); } Justina Balsė 13 Logical Operators Justina Balsė 14 SWITCH let a = 2 + 2; The switch has one or more case blocks and an switch (a) { optional default. case 3: alert( 'Too small' ); Here the switch starts to compare a from the break; first case variant that is 3. The match fails. case 4: alert( 'Exactly!' ); Then 4. That’s a match, so the execution starts break; case 5: from case 4 until the nearest break. alert( 'Too large' ); break; If there is no break then the execution default: continues with the next case without any alert( "I don't know such values" ); checks. } Justina Balsė 15 Praktika Uždavinių sprendimas konsolėje. 02 - Šakotieji algoritmai (PraktikaEN) 02 - Šakotieji algoritmai (PraktikaLT) Pasitikriname kodą: https://jshint.com/ 16 Functions | Example 17 Falsy values | 1 false A falsy (sometimes written null falsey) value is a value that is undefined considered false when 0 encountered in a Boolean context. -0 NaN "" Justina Balsė 18 Falsy values | 2 var age = 0; if(age){ console.log("Variable is defined.") } else{ console.log("Variable has NOT been defined.") } Justina Balsė 19 Falsy values | 3 var age = 0; if(age || age === 0){ console.log("Variable is defined.") } else{ console.log("Variable has NOT been defined.") } Justina Balsė 20 Falsy values | 4 let city; Console: let defaultCity = "Vilnius"; let myCity = city || defaultCity; // 1 console.log(myCity); // 1 Vilnius city = "Kaunas"; // 2 myCity = city || defaultCity; Kaunas console.log(myCity); // 2 Justina Balsė 21 Logical Operator || OR console.log(true || false); // 1 console.log(false || true); // 2 console.log(false || false || true); // 3 console.log("" || "abc"); // 4 console.log("abc" || ""); // 5 // Boolean("") -> false // Boolean("abc") -> true Justina Balsė 22 Logical Operator || OR console.log(true || false); // 1 1. true console.log(false || true); // 2 console.log(false || false || true); // 3 2. true console.log("" || "abc"); // 4 3. true console.log("abc" || ""); // 5 4. abc // Boolean("") -> false 5. abc // Boolean("abc") -> true Justina Balsė 23 Logical Operator && AND console.log(true && false); // false console.log(false && true); // false console.log(false && false && true); // false console.log("abc" && 10 && false && "abcd"); // false console.log("abc" && 10 && NaN && "abcd"); // NaN console.log("abc" && 10 && "def" && "Hello"); // Hello Justina Balsė 24 Short-circuit evaluation && AND Rezultatas: let isAdmin1 = true; isAdmin1 == true && console.log("Admin1 page"); Admin1 page let isAdmin2 = true; Admin2 page isAdmin2 && console.log("Admin2 page"); Justina Balsė 25 Kartojame Vaizdo įrašas apie Short Circuiting: https://www.youtube.com/watch?v=O4QKfjh8jJM Javascript If Else Statements 26