JavaScript – If Block

Control Statements:

  • Statement is a line of code.
  • Sequential Statements execute one by one from top to bottom.
  • Control statements execute repeatedly and randomly based on conditions
  • Conditional Statements are if, if-else, if-else-if, nested-if and switch

If Block: Executes a block of instructions if the given condition is valid.

Program: Provide 15% discount to customer on bill amount if the bill amount is >5000

<html>
            <head>
                        <script>
                                    function discount() {
                                                var bill = parseFloat(document.getElementById(“bill”).value);
                                                if(bill>5000) {
                                                            var disc = 15/100 * bill ;
                                                            bill = bill-disc ;
                                                }
                                                document.write(“Final bill to pay : ” + bill);
                                    }
                        </script>
            </head>
            <body>
                        Enter Bill Amount :       <input type=”text” id=”bill”/> <br/>
                        <button onclick=”discount()”> Final Bill to Pay </button>
            </body>
</html>
Scroll to Top