There is one interesting property about if...else that if you only have one line of code to be executed in the if-block or the else-block, then you can skip the curly braces. Let’s understand this in detail with an example.
In the last video, you learnt that if the "if" or "else" blocks contain only one statement to be executed, you can skip the curly braces. But remember, you can skip the curly braces only for one statement and not for multiple.
Note: One statement doesn’t mean one line. So if you write something like:
if(true) console.log('a'); console.log('b'); else console.log('c');
The above code will result in an error similar to the one you saw in the previous video above. This is because of the if-block having multiple statements inside it without being enclosed within curly braces.
Let's now learn about an important problem that occurs (mostly when not using curly braces) in the conditional statement. This problem is called the 'dangling else' problem.
The dangling else problem exists when the translator is not able to determine which if block the else block belongs to.