How to redeclare variables in switch block without an error?

Posted by Andre Scott | Updated on

How to redeclare variables in switch block without an error?

If you try to redeclare variables in a switch block then it will cause errors because there is only one block. For example, the below code block throws a syntax error as below,

To avoid this error, you can create a nested block inside a case clause and create a new block scoped lexical environment.


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers