Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
-1
rated 0 times [  6] [ 7]  / answers: 1 / hits: 129696  / 11 Years ago, wed, july 3, 2013, 12:00:00
<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?


More From » javascript

 Answers
39

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.


[#77238] Tuesday, July 2, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
montana

Total Points: 675
Total Questions: 86
Total Answers: 102

Location: Mali
Member since Fri, Dec 3, 2021
3 Years ago
montana questions
Sat, Sep 19, 20, 00:00, 4 Years ago
Fri, Jan 31, 20, 00:00, 4 Years ago
Thu, Sep 12, 19, 00:00, 5 Years ago
;