DEV Community

Discussion on: Time conversion

Collapse
 
kecbm profile image
Klecianny Melo

Hello @jdlarsen1945, how are you? In JavaScript, the const keyword is used to declare constants, which means that the variable cannot be reassigned. However, it does not mean that the value itself is immutable. In this code snippet, the variable hour is initially declared using const, but the value of hour is being updated in the subsequent lines of code based on conditions.

The initial declaration with const only prevents reassignment of the variable hour, not the modification of the value itself. So, in the third line, hour is being reassigned a new value based on a conditional expression, which is allowed even though it was originally declared as a constant.

In summary, while const prevents reassignment of variables, it does not make the value immutable. Therefore, the third line of the code snippet is valid and compiles without any issues 😁

Collapse
 
jdlarsen1945 profile image
James Larsen

Sorry, I am not Java Programmer and I didn't know the code was in Java. (I am a C, C+, C++, C# programmer) In those languages, Constants are constant! and not Immutable. Sorry for the confusion, but in my world constant means immutable, the exact opposite of variable! Thank you for the education!

Thread Thread
 
kecbm profile image
Klecianny Melo

Alright @jdlarsen1945, I thank you for your contribution to the article. Every discussion is valid, we can always learn something new together. I look forward to seeing you in the next posts so we can reflect on code or a career in technology! 😁

Collapse
 
rcalvanese profile image
rcalvanese

Actually, I am pretty sure this will error. You cannot reassign a value to hour once it has been assigned the value of "07". "Uncaught SyntaxError: Identifier 'hour' has already been declared"

Thread Thread
 
kecbm profile image
Klecianny Melo

Hi @rcalvanese, how are you? How did you run the code? Was it with another string for the time in the constant "s"?

Thread Thread
 
rcalvanese profile image
rcalvanese

Hi! I ran it here jsfiddle.net/cx9pa41n/

Thread Thread
 
kecbm profile image
Klecianny Melo

Hi! I understood. You can replace const with var in this line to resolve this error: var [hour, minute, second] = s.slice(0,8).split(':');