Skip to content
Related Articles

Related Articles

Improve Article

JavaScript | Check if a variable is a string

  • Difficulty Level : Medium
  • Last Updated : 15 Apr, 2019

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:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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

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




<!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>

Output:

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

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




<!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>

Output:

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :