JavaScript typeof

We can use typeof function to know about type of variable used. This function tells us that the variable is a string or number or Boolean or any object. Most important is getting the output as 'undefined' which tells us that the variable does not exist. So we can take corrective action.

Here are some examples.
<script type="text/javascript">
function my_fun(){

var v1= new my_fun();
document.write(typeof(v1) + "<br>"); // output is object

var v2= "plus2net";
document.write(typeof(v2) + "<br>"); // output is string

var v3= 5.73;
document.write(typeof(v3) + "<br>"); // output is number

var v4= 18
document.write(typeof(v4) + "<br>"); // output is number

var students= new Array;
document.write(typeof(students) + "<br>"); // output is object

var v5 = false;
document.write(typeof(v5) + "<br>"); // output is boolean

document.write(typeof(v6) + "<br>"); // output is undefined

We will use typeof() after converting a string variable to number by using parseFloat
var v1 = "5.25";
document.write(typeof(v1) + "<br>"); // output is  string

document.write(typeof(v1) + "<br>"); // output is number

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here .

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer