Tuesday 28 August 2018

JavaScript: get the type of variable

Javascript provide typeof operator, to get the type of a variable.

Syntax
typeof variableName

type.html
<!DOCTYPE html>

<html>

<head>
    <title>Type of variables</title>
</head>

<body>
    <script type="text/javascript">
        var a = 10;
        var b = 10.09;
        var c = true;
        var d = "Hello";
        var e = new String("Hello");

        document.write("a = " + a + ", type = " + typeof(a) + "<br />");
        document.write("b = " + b + ", type = " + typeof(b) + "<br />");
        document.write("c = " + c + ", type = " + typeof(c) + "<br />");
        document.write("d = " + d + ", type = " + typeof(d) + "<br />");
        document.write("e = " + e + ", type = " + typeof(e) + "<br />");
    </script>
</body>

</html>



Previous                                                 Next                                                 Home

No comments:

Post a Comment