JavaScript | Check if a variable is a string

Checking the type of a variable can be done by using typeof operator. It directly applies either to a variable name or to a variable.

Syntax:

typeof varName;
  • varName: It is the name of variable.

Example-1:This Example checks if the variable boolValue and numValue is string.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        Javascript | Check if a variable is a string
    </title>
</head>
  
<body style="text-align:center;">
  
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p>
        var boolValue = true;
        <br> var numValue = 17;
    </p>
  
    <button onclick="Geeks()">
        Click to check
    </button>
    <p id="GFG_P" style="color:green; font-size: 20px;">
    </p>
    <script>
        function Geeks() {
            
            <!-- "boolean" value. -->
            var boolValue = true;
              
            <!-- "integer " value. -->
            var numValue = 17;
            var el = 
                document.getElementById("GFG_P");
            var bool, num;
            
            if (typeof boolValue == "string") {
                bool = "is a string";
            } else {
                bool = "is not a string";
            }
            if (typeof numValue == "string") {
                num = "is a string";
            } else {
                num = "is not a string";
            }
            el.innerHTML = "boolValue " + bool 
            + "<br>numValue " + num;
  
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Example-2:This Example checks if the variable strValue and objGFG is string.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        Javascript | Check if a variable is a string
    </title>
</head>
  
<body style="text-align:center;">
  
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p>
        <!-- "String" value. -->
        var strValue = 
      "This is GeeksForGeeks";
        <br
      <!-- "Object" value. -->
      var objGFG = new String( 
      "This is GeeksForGeeks" );
    </p>
  
    <button onclick="Geeks()">
        Click to check
    </button>
    <p id="GFG_P" style="color:green; font-size: 20px;">
    </p>
    <script>
        function Geeks() {
            var strValue = "This is GeeksForGeeks";
            var objGFG = new String("This is GeeksForGeeks");
            var el = document.getElementById("GFG_P");
            var str, obj;
            if (typeof strValue == 'string') {
                str = "is a string";
            } else {
                str = "is not a string";
            }
            if (typeof objGFG == "string") {
                obj = "is a string";
            } else {
                obj = "is not a string";
            }
            el.innerHTML = "strValue " + str + "<br>objGFG " + obj;
  
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.