I'm new in javascript and are just checking out some stuff other people have created, I can't figure out why they need this variable for this script, but when I remove it. The script stop working. <HTML> <BODY> <SCRIPT STYLE = "text/javascript"> var d = new Date() var time = d.getHours() if (time < 10) { document.write("Good morning") } </SCRIPT> <P>This is a simple script that will say: "Good morning" if the time is less than 10</P> </BODY> </HTML> Code (markup):
what part aren`t you understanding it ? I`m a very newbie at js, but it`s logic var d = new Date() var time = d.getHours() if (time < 10) { document.write("Good morning") } time gets the time and them you check if the time it`s lower than 10. if it is, then it`s morning.
Sorry, maybe I was a bit unclear on my post But I'm getting what the script does, what I'm not getting is what "var d = new Date()" is needed for. After all the "time" variable is the only one I can see being used.
The date variable "d" is used to create the time variable. var time = d.getHours() Code (markup): That d is referencing to the var d = new Date(); Code (markup): If you only wanted one variable you could also do the following var time = new Date().getHours() Code (markup):
The statement var d = new Date() creates a new Date object and sets it to the current time and date. Then it fetches the hour from the time and puts in the time variable. If the hour is less than 10 , that is if the time is before 10 AM, it says Good Morning. You can look up the properties of the Date object at: http://www.w3schools.com/jsref/jsref_obj_date.asp They also have a Javascript tutorial at: http://www.w3schools.com/js/ Thomas
by defining variables as objects, you have access to their properties and functions... objects are neato!