# JAVASCRIPT ## JAVASCRIPT OPERATOR In javascript we have the following operators * Assingnment operator: This is used in the assignment of value to a variable. E.g let price = 1200 let total = x+y String assignment: EG ```` <!DOCTYPE html> <html> <head> <title>javascript</title> </head> <body> <h1 id="demo"> </h1> <script> let text = "hello"; text += "world"; document.getElementById("demo").innerHTML </script> </body> </html> This code above shows how string assignment is done. ```` **The &&= Operator** The Logical AND assignment operator is used between two values. this is used to check two conditions. Both mus be true when one is false all the will print false ```` <script> let age = 10; let hasID = true; if (age>= 18 && hasID === true){ console.log("you can enter") } </script> ```` We have other operators like "OR operator and the ?? operator". ```` <script> let name = null; let displayname = name ?? "guest" console.log(displayname) </script> Here since name is null it it will take the one on the right hand which is guest. ```` ## COMPARISM OPERATORS Comparison operators are used to compare two values. Comparison operators always return true or false. Some comparism operators are the following ``` let price = 10; let age = 10; if(age==price){ console.log("we are equal") } 2nd let car = "10" let home = "20" //this is is not if(car != home){ console.log("were not equal") } // !== this one is : not equal or note equaletype === this is they mus be equaland same datatype ```` ## JAVASCRIPT CONDITION. In javascript we have the following condition statement: * If Statement * if...else * if...else if...else * switch * ternary (? :) When to use Conditionals * Use if to specify a code block to be executed, if a specified condition is true * Use else to specify a code block to be executed, if the same condition is false * Use else if to specify a new condition to test, if the first condition is false * Use switch to specify many alternative code blocks to be executed * Use (? :) (ternary) as a shorthand for if...else **IF STATEMENT** Use the JavaScript if statement to execute a block of code when a condition is true. ```` <p id="demo"></p> <script></script> if (hour < 18){ document.getElementById("demo").innerHTML = "GOOD DAY" //NESSTED IF Nested if statements can make your code more complex. A better solution is to use the logical AND operator: //USING NESSTED IF <script> let age = 18 let country = nigeria let text = "you can drive" if(age>=18){ if (country == nigeria){ text = "you can drive" } } document.getElementById("demo").innerHTML=text; //USING LOGICAL OPERATOR let age = 18 let country = nigeria let textt = "you can drive " if(age>=18 && country ==nigeria){ text= "you can drive" } document.getElementById("demo").innerHTML = text; </script?> ```` The else Statement Use the else statement to specify a block of code to be executed if a condition is false. ```` if (hour < 18) { greeting = "Good day"; } else { greeting = "Good evening"; } ```` The Else...if Statement ```` <p id="demo"></p> <script> const time = new Date().getHours(); let greetings; if (time < 10) { greeting = "Good morning"; } else if (time < 20) { greeting = "Good day"; } else { greeting = "Good evening"; } document.getElementById("demo").innerHTML = greeting; </script> ```` **TENARY** This is used in place of if...else and it is very common in modern javascript like react. ```` <script> let price = 10 let ismember = true let discount = ismember ? 0.2:0 //thiis means discount for members and no dicount for nonn memebers. let total = 10 - (price * discount); document.getElementById("demo").innerHTML = "The price is " + total; </script> ```` ## JavaScript Switch Statement Based on a condition, switch selects one or more code blocks to be executed. switch executes the code blocks that matches an expression. switch is often used as a more readable alternative to many if...else if...else statements, especially when dealing with multiple possible value ```` let day; let date = new Date().getDay(); switch (date){ case 0 day = "Sunday"; break; case 1 day = "Monday"; break; case 2 day = "Tuesday"; break; case 3 day = "Wednesday" break case 4 day = "thursday" break case 5 day = "Friday"; break; case 6 day = "saturday" break default: day = "looking forward to hear from you" } document.getElementById("demo").innerHTML = "Today is " + day; ````