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> |
chevron_right
filter_none
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> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to check if a variable is an array in JavaScript?
- JavaScript | Check the existence of variable
- How to check a variable is of function type using JavaScript ?
- JavaScript | Check if a string is a valid JSON string
- variable === undefined vs. typeof variable === âundefinedâ in JavaScript
- How to check if a string is html or not using JavaScript?
- How to check if string contains only digits in JavaScript ?
- How to check a string is entirely made up of the same substring in JavaScript ?
- JavaScript | Check whether a URL string is absolute or relative
- JavaScript | Check if a string is a valid hex color representation
- How to check empty/undefined/null string in JavaScript?
- JavaScript | Set object key by variable
- Understanding variable scopes in JavaScript
- How to use dynamic variable names in JavaScript ?
- JavaScript ReferenceError - variable is not defined
- How to use a variable for a key in a JavaScript object literal?
- JavaScript SyntaxError - Missing variable name
- How to check whether a variable is set or not using PHP ?
- JavaScript SyntaxError "variable" is a reserved identifier
- JavaScript ReferenceError - Assignment to undeclared variable
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.


