Javascript let keyword

class Thermostat {

constructor(fahrenheit) {

this.fahrenheit = fahrenheit;

}

get temperature() {

return (5 / 9) * (this.fahrenheit - 32);

}

set temperature(celsius) {

this.fahrenheit = (celsius * 9.0) / 5 + 32;

}

}

const thermos = new Thermostat(76); // Setting in Fahrenheit scale

console.log(thermos.temperature);

let temp = thermos.temperature; // 24.44 in Celsius
thermos.temperature = 26;
temp = thermos.temperature;

I thought the let keyword is used instead of the var so as to prevent variables from overwriting :thinking: :thinking:.or I dont just understand the use of let in the last few lines of the code.

The let keyword is actually used in order to allow the value of the variable to change. The advantages of using let over var have to do with scope. If you don’t want to allow the value of a variable to change then use const. And you can basically forget that var even exists as most people will recommend you never use it.

4 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.