Cool snippets by the way it's better to put that code inside of Markdown Fenced Code Blocks with Syntax Highlighting. It would make them all far more readable.
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
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.
Top comments (9)
Cool snippets by the way it's better to put that code inside of Markdown Fenced Code Blocks with Syntax Highlighting. It would make them all far more readable.
markdownguide.org/extended-syntax/
Thanks for the suggestion. Will be changing it ASAP.
Nice. You can also mention destructuring.
Thanks for the suggestion. I shall look into it in Imp JS Shorthands to know Part2!
Hi Arjun I'm beginner can you explain to me something about programming? I have a simple doubt, Why you declared let answer?
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
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");
}
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.
Alright