What Happens If a Semicolon is Misplaced in JavaScript



If a semicolon is misplaced in JavaScript, then it may lead to misleading results. Let’s see an example, wherein the if statement condition is false, but due to misplaced semi-colon, the value gets printed.

Example

<!DOCTYPE html>
<html>
   <body>
      <script>
         var val1 = 10;

         if (val1 == 15) {
            document.write("Prints due to misplaced semi-colon: "+val1);
         }
         var val2 = 10;

         if (val2 == 15) {
            // this won't get printed
            document.write(val2);
         }
      </script>
   </body>
</html>
Updated on: 2020-05-20T09:04:03+05:30

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements