JavaScript – If Else Block

if – else block: Else block can be used to execute an optional logic if the given condition has failed.

Program to check the person is eligible for Vote or Not:

            <head>
                        <script>
                                    function canVote(){
                                                var age = paseInt(document.getElementById(“age”).value);
                                                if(age>=18)
                                                            document.write(“you can vote”);
                                                else
                                                            document.write(“wait ” + (18-age) + ” years to vote”);
                                    }
                        </script>
            </head>
            <body>
                        Enter your Age :           <input type=”text” id=”age”/><br/>
                        <button onclick=”canVote()”> can vote </button>
            </body>

Program to check the number is Even or Not:

            <head>
                        <script>
                                    function isEven(){
                                                var num = parseInt(document.getElementById(“num”).value);
                                                if(num%2==0)
                                                            document.write(num + ” is even”);
                                                else
                                                            document.write(num + ” is not even”);
                                    }
                        </script>
            </head>
            <body>
                        Enter number : <input type=”text” id=”num”/><br/>
                        <button onclick=”isEven()”> Even or Not </button>
            </body>
</html>
Scroll to Top