?Utilizzare if per specificare un blocco di codice da eseguire, se una condizione specificata ? vera 'Utilizzare else per specificare un blocco di codice da eseguire, se la stessa condizione ? falsa 'Utilizzare else if per specificare una nuova condizione da testare, se la prima condizione ? falsa 'Utilizzare switch per specificare molti blocchi di codice alternativi da eseguire Nota che if ? in lettere minuscole. Le lettere maiuscole (If o IF) generano un errore JavaScript. https://www.w3schools.com/js/tryit.asp?filename=tryjs_ifthen <!DOCTYPE html> <html> <body> <p>Display "Good day!" if the hour is less than 18:00:</p> <p id="demo">Good Evening!</p> <script> if (new Date().getHours() < 18) { document.getElementById("demo").innerHTML = "Good day!"; } </script> </body> </html> Se l'ora ? inferiore a 18, crea un saluto "Buona giornata", altrimenti "Buonasera": https://www.w3schools.com/js/tryit.asp?filename=tryjs_ifthenelse <!DOCTYPE html> <html> <body> <p>Click the button to display a time-based greeting:</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var hour = new Date().getHours(); var greeting; if (hour < 18) { greeting = "Good day"; } else { greeting = "Good evening"; } document.getElementById("demo").innerHTML = greeting; } </script> </body> </html> ' Utilizzare l' else if istruzione per specificare una nuova condizione se la prima condizione ? falsa. https://www.w3schools.com/js/tryit.asp?filename=tryjs_elseif <!DOCTYPE html> <html> <body> <p>Click the button to get a time-based greeting:</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var greeting; var time = new Date().getHours(); if (time < 10) { greeting = "Good morning"; } else if (time < 20) { greeting = "Good day"; } else { greeting = "Good evening"; } document.getElementById("demo").innerHTML = greeting; } </script> </body> </html> https://www.w3schools.com/js/tryit.asp?filename=tryjs_best_parameter_default <!DOCTYPE html> <html> <body> <p>Setting a default value to a function parameter.</p> <p id="demo"></p> <script> function myFunction(x, y) { if (y === undefined) { y = 0; } return x * y; } document.getElementById("demo").innerHTML = myFunction(4); </script> </body> </html> ' |