JavaScript | Check if a variable is a string - GeeksforGeeks

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:
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:
javascript-basics
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?