What Is The Best Way To Check Variable Type in Javascript - Stack Overflow
What Is The Best Way To Check Variable Type in Javascript - Stack Overflow
What Is The Best Way To Check Variable Type in Javascript - Stack Overflow
<script type="text/javascript">
function saveName (firstName) {
15 function capitalizeName () {
return firstName.toUpperCase();
}
var capitalized = capitalizeName();console.log(capitalized instanceof String);
return capitalized;
}
4 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?
javascript
1 Because a string literal isn't an object of String type. See typeof capitalized – zerkms Jul 3 '13 at
5:44
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.
Join Stack Overflow to learn, share knowledge, and build your career. Sign up
Share Improve this answer Follow edited Jul 8 '13 at 4:09 answered Jul 3 '13 at 5:45
LandonSchropp
8,902 16 71 142
typeof new String("hello") == "object" but it can benefit from all the same characteristics of
string . It would be safest to say typeof x == "string" || x instance of String – Brian Nickel ♦ Jul
3 '13 at 5:49
I suppose you're correct, although in practice I almost never see new String("hello") used. –
LandonSchropp Jul 3 '13 at 5:55
function is not a built-in type. A function is an object. And null is a type too. See ECMAScript specs,
sections 8 and 11.4.3 – a better oliver Jul 3 '13 at 6:14
use typeof();
2 example:
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;
}
Share Improve this answer Follow edited Jul 3 '13 at 5:53 answered Jul 3 '13 at 5:46
Fasil kk
2,006 13 23
1 typeof "blahha"
I made a function with help of jQuery library code, jQuery library type method github link .
Share Improve this answer Follow edited Jul 3 '13 at 6:24 answered Jul 3 '13 at 5:51
rab
3,902 1 27 39
The getVarType method (below) works for almost all variables. Check out this fiddle. It first uses
the very fast typeof for cases where the results are reliable. Then it uses a more expensive
1 toString method for other cases. Finally, if it is dealing with a named object (as returned by
Join Stack Overflow to learn, share knowledge, and build your career. Sign up
Firefox for objects like document.location) it checks for Array-like objects and reports them as
arrays.
'Undefined' : 'Undefined',
'Null' : 'Null',
'Boolean' : 'Boolean',
'Number' : 'Number',
'String' : 'String',
'Function' : 'Function',
'Array' : 'Array',
'StyleSheetList' : 'Array'
};
The only possible failure mode happens if you are testing a named array that is empty (e.g. an
empty enumerable DOM object besides the StyleSheetList). But on could add those to the
type_of_map as needed.
Share Improve this answer Follow edited Jul 11 '13 at 20:36 answered Jul 11 '13 at 1:27
Michael Mikowski
1,229 1 10 19
Join Stack Overflow to learn, share knowledge, and build your career. Sign up
0
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-06-09
* @modified
*
* @description js data type checker
* @augments
* @example
* @link
*
*/
export {
dataTypeChecker,
};
test
Join Stack Overflow to learn, share knowledge, and build your career. Sign up
const obj = {};
https://stackoverflow.com/questions/17440517/what-is-the-best-way-to-check-variable-type-in-javascript#:~:text=The best way is to use the typeof key… 5/6
2/17/2021 what is the best way to check variable type in javascript - Stack Overflow
const func = () => {};
dataTypeChecker(NaN)
//"[object Number]"
dataTypeChecker(undefined)
//"[object Undefined]"
dataTypeChecker(true)
//"[object Boolean]"
dataTypeChecker({})
//"[object Object]"
dataTypeChecker(func)
//"[object Function]"
dataTypeChecker(obj)
//"[object Object]"
dataTypeChecker(Symbol())
//"[object Symbol]"
dataTypeChecker(null)
//"[object Null]"
dataTypeChecker(123)
//"[object Number]"
dataTypeChecker(BigInt(1n))
//"[object BigInt]"
Join Stack Overflow to learn, share knowledge, and build your career. Sign up