Home » JavaScript

JavaScript | Example of if with else

JavaScript Example of if with else: Here, we have an example in which we are demonstrating an example of if else in JavaScript.
Submitted by Pankaj Singh, on October 22, 2018

Example:

In this example, we are reading two integer values (through HTML) and finding the greatest/largest number among them.

Code (JS & HTML):

<!DOCTYPE html>

<HTML>
    <HEAD>
        <SCRIPT>
            function Find(){
                var a=parseInt(document.getElementById("txta").value);
                var b=parseInt(document.getElementById("txtb").value);
                var g=0;
                if(a>b){
                    g=a;
                }
                else{
                    g=b;
                }
                document.getElementById("txtc").value=""+g;
            }
        </SCRIPT>
    </HEAD>
    <BODY>
        <h2>If with Else</h2>
        <hr />
        <table>
            <tr>
                <td>
                    <label>Enter A:</label>
                </td>
                <td>
                    <input type="text" name="txta" id="txta" />
                </td>
            </tr>
            <tr>
                <td>
                    <label>Enter B:</label>
                </td>
                <td>
                    <input type="text" name="txtb" id="txtb" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    <input type="button" value="Find" onclick="Find()" />
                </td>
            </tr>
             <tr>
                <td>
                    <label>Greater</label>
                </td>
                <td>
                    <input type="text" name="txtc" id="txtc" readonly />
                </td>
            </tr>
        </table>
    </BODY>
</HTML>

Output

if with else example | js






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.