<script type="text/javascript"> function saveName (firstName) { function capitalizeName () { return firstName.toUpperCase(); } var capitalized = capitalizeName();console.log(capitalized instanceof String); return capitalized; } console.log(saveName("Robert")); // Returns "ROBERT" </script>
Question:
I want to check the type of capitalized , so I use capitalized instanceof String
? But it shows: false
in console, I do not want to try capitalized instanceof Function
, Object
...It will take too much time, so what is the best way to detect a variable type?
typeof: The typeof keyword helps to determine the type of a variable in Javascript. Since Javascript is a dynamically typed programming language, typeof can be used to find the variable type. It can be used within a function to check the data type of a variable or to check if a variable is declared.
To check the type of a variable, you can use the type() function, which takes the variable as an input. Inside this function, you have to pass either the variable name or the value itself. And it will return the variable data type.
Variables can be used to store data in a program, such as strings, numbers, JSON objects, or boolean values. In JavaScript, there are three different variable types: var , let , and const . Each of these variables have several rules around how they should be used, and have different characteristics.
To check if a variable is of type function, use the typeof operator, e.g. typeof myVariable === 'function' . The typeof operator returns a string that indicates the type of the value. If the type of the variable is a function, a string containing the word function is returned. Copied!
let's say that you want to test or check if a variable is a string, see the code bellow As you can see, the code works very well, but this is not the best way to do that. Remember!, if you want to check the type of variable, you should use isinstance () built function.
As you can see, the code works very well, but this is not the best way to do that. Remember!, if you want to check the type of variable, you should use isinstance () built function.
Using instanceof operator: The instanceof operator checks the type of an object at run time. It return a corresponding boolean value, i.e, either true or false to indicate if the object is of a particular type or not.
Javascript is known to have no types for variables. A really nice thing. But somehow it must determine the type and work with it. var a = 1; var b = 2.0; var c = 'c'; var d = "Hello World!"; So what we have is an Integer, Double/Float, Character, String (which may be teared down as char*)
The best way is to use the typeof
keyword.
typeof "hello" // "string"
The typeof
operator maps an operand to one of six values: "string"
, "number"
, "object"
, "function"
, "undefined"
and "boolean"
. The instanceof
method tests if the provided function's prototype is in the object's prototype chain.
This Wikibooks article along with this MDN articles does a pretty good job of summing up JavaScript's types.
use typeof();
example:
> typeof "foo" "string" > typeof true "boolean" > typeof 42 "number"
So you can do:
if(typeof bar === 'string') { //whatever }
Keep in mind that, typeof is only good for returning the "primitive" types, number, boolean, object, string. You can also use instanceof to test if an object is of a specific type.
function MyObj(prop) { this.prop = prop; } var obj = new MyObj(10); console.log(obj instanceof MyObj && obj instanceof Object); // outputs true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With