DEV Community

Discussion on: Important JavaScript Shorthands to know 🚀🔥

Collapse
 
arjuncodess profile image
Arjun Vijay Prakash

Hi Kilson, I didn't assigned the value to answer because you see, I changed its value afterwards. if I don't assign it and change the code as follows it will not run the same.
const x = 20;
if (x > 10) {
let answer = "greater than 10";
} else {
let answer = "less than 10";
}
As there will be one problem, we will not be able to access the answer variable because it's scope is inside the if-else statement and the error will be thrown as "answer is not defined"
See this - dev-to-uploads.s3.amazonaws.com/up...
We can correct the program as- dev-to-uploads.s3.amazonaws.com/up...

--Conclusion--
I used let answer; to shorten my code and to provide the audience with better and cleaner code.
Thanks! I hope you got the answer

Collapse
 
kilsonjs profile image
Kilson

I tried this way and it worked too

const x = 30;
if (x > 10) {
console.log("greater than 10");
} else {
console.log("less than 10");
}

Thread Thread
 
arjuncodess profile image
Arjun Vijay Prakash

Yeah. But I assigned the value to the variable that's why I mentioned that ways. Although that is also correct. Thanks for connecting with me. It was a great pleasure.

Thread Thread
 
kilsonjs profile image
Kilson

Alright