What will happen if a semicolon is misplaced in JavaScript?

HTMLJavascriptProgramming Scripts

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>
raja
Published on 26-Feb-2018 09:48:41
Advertisements