Everything between the double quotes in document.write() is printed onto the page. The above example starts a new paragraph and displays some centered text. All HTML is accepted inside document.write().
We now have two pieces of knowledge: how to get the current time in hours and how to print out onto the page. But how do you combine the two? By using something called if statements.
It looks like fractured English, but there is a method to this madness. All if statements consist of the word if followed by a statement in parentheses and a block of code in braces. The parentheses contain the condition that is being checked. The braces contain the code that will run if the condition is met.
Lets just take a deeper look into the code above. The first line says, if the value of the variable hour is greater than 4 and less than 18, then run the code in the braces. (Note: I’m sure you remember the < (less than) and > (greater than) symbols from math class, but what the heck is &&? Well the && means and. Now what happens if it is 7pm? Since we weren’t testing for this time, the else statement applies. With else, we’re saying “If the condition isn’t true, then do this instead“. If its 7pm, then its not between 4am and 6pm, so the script runs the code following the word else.
Hope you have enjoyed this tutorial! Good luck!