As the name indicates this inbuilt javascript function is used to return the type of the datatype. It returns a string value and accept variable as the parameter

Syntax of typeof is

typeof(varablename)

This function returns the following values

number

string

boolean

function

Also it returns undefined as the value when we are trying to use type of with an undefined variable or function.

Number

<script type="text/javascript">

var num=12;

alert(typeof(num));

</script>

string

<script type="text/javascript">

var str="23";

alert(typeof(str));

</script>

boolean

<script type="text/javascript">

var bln=true;

alert(typeof(bln));

</script>

function

<script type="text/javascript">

function test()

{

}


alert(typeof(test));

</script>


undefined

<script type="text/javascript">

alert(typeof(num2));

</script>

last one can be a pretty useful one while coding. We can check whether a variable is defined within a particular scope using this

Say

<script type="text/javascript">

if(typeof(num)!="undefined");

{

//Some code } </script>

0 comments