Type Casting

Class: B.Com II yr
Subject: Web Designing
Subject Type: OEL
Asst.Prof.Manju Kumawat

Typecasting in JavaScript is the conversion of one data type into another data type, for example, a number to a string. Typecasting is often also known as type conversion.

Example
<!DOCTYPE html>
<html>
<head>
    <title>Type Casting Example</title>
</head>
<body>
    <h2>Simple Addition Example</h2>

 First number: <input type="text" id="num1"><br><br>
    Second number: <input type="text" id="num2"><br><br>
    Third number: <input type="text" id="num2"><br><br>
    <button onclick="add()">Add</button>

   <script>
        function add() {
            // Input values ko read karna
            a = document.getElementById("num1").value;
            b = document.getElementById("num2").value;
            c=document.getElementById("num2").value

            // String to number conversion
            a = Number(a);
            b = Number(b);
            c = Number(c);

            // Addition
            sum = a + b+c;

            // Result show 
            document.write("Sum: " + sum);
        }
    </script>
</body>
</html>

0 comments:

Post a Comment