3 Example(s) of JavaScript typeof operator


Description :

TypeOf keyword is used to get type of provided variable. So it will return number because value of val is 5.


JavaScript typeof operator Example - 1
<!DOCTYPE html>
<html>
<head>
    <title>Welcome to LearnKode - A code learning platform</title>
    
</head>

<body>
    <button onclick="operatorFunction()">Click to find the type of variable.</button>
    <script>
    function operatorFunction()
    {
    var val=5;
    alert(typeof val);  
    }
    </script>
</body>
</html>

Output

Description :

TypeOf keyword is used to get type of provided variable. So it will return string and will print true.


JavaScript typeof operator Example - 2
<!DOCTYPE html>
<html>
<head>
    <title>Welcome to LearnKode - A code learning platform</title>    
</head>
<body>
       <script>
             var description = "abcdfghjk";
             var result = (typeof description === 'string');
             document.write(result);
    </script>
</body>
</html>

Output

Description :

TypeOf will return undefined and will print true as comparing to undefined will return true.


JavaScript typeof operator Example - 3
<!DOCTYPE html>
<html>
<head>
    <title>Welcome to LearnKode - A code learning platform</title>    
</head>
<body>
     <script>
var obj = {};
var result = (typeof obj.propNotDeclared === 'undefined');
document.write(result);
    </script>
</body>
</html>

Output