Click here to Skip to main content
15,352,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was making a basic alarm in JS and I was following a tutorial and while assigning "now" the programmer dint use any let or const variable and the code is still working fine(the line which is commented out with "Here")...Can someone explain why the code is working without using let or const to initiate the variable?

What I have tried:

<pre>function setAlarm(e) {
    e.preventDefault();
    const alarm = document.getElementById('alarm');
    alarmDate = new Date(alarm.value);
    console.log(`Setting Alarm for ${alarmDate}...`);

 here//   now = new Date();

    let timeToAlarm = alarmDate - now;
    console.log(timeToAlarm);
    if(timeToAlarm>=0){
        setTimeout(() => {
            console.log("Ringing now")
            ringBell();
        }, timeToAlarm);
    }
}
Posted
Updated 7-Feb-22 4:12am

1 solution

Javascript allows different forms of assignment, as described at JavaScript Variables[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900