Change images on my webpage base on time and day

Yes you are correct the single ‘=’ is an assignment operator and assigns the value on the right to the variable on the left.

Therefore this code:

if (hour = 19) {
	alert(hour);
	} else {
	//this will never be executed
      alert('help');

}

The above will always result in the hour being 19 because that’s what you suddenly assign to it and so it always will be true.

On the other hand the double == is a comparison operator and will compare the value on the right to the variable on the left…


var date = new Date();
	    var day = date.getDay();
	    var hour = date.getHours();
	    var minutes = date.getMinutes();
	    var img;

if (hour == 19) {
	do something 
	} else {
	do something else
}

The following code will only parse the first section (do something) when the actual hour is 19.

2 Likes