MOCKSTACKS
EN
Questions And Answers

More Tutorials









Javascript Datatypes

Typeof

typeof is the 'official' function that one uses to get the type in JavaScript, however in certain cases it might yield some unexpected results ...

1. Strings


typeof "String" or
typeof Date(2011,01,01)

2. Numbers


typeof 42

3. Bool


typeof true (valid values true and false)

4. Object


typeof {} or
typeof [] or
typeof null or
typeof /aaa/ or
typeof Error()

5. Function


typeof function(){}

6. Undefined


var var1; typeof var1

Finding an object's class

To find whether an object was constructed by a certain constructor or one inheriting from it, you can use the instanceof command:


function sum(...arguments) {
 if (arguments.length === 1) {
 const [firstArg] = arguments
 if (firstArg instanceof Array) { //firstArg is something like [1, 2, 3]
 return sum(...firstArg) //calls sum(1, 2, 3)
 }
 }
 return arguments.reduce((a, b) => a + b)
}
console.log(sum(1, 2, 3)) //6
console.log(sum([1, 2, 3])) //6
console.log(sum(4)) //4

Note that primitive values are not considered instances of any class:


console.log(2 instanceof Number) //false
console.log('abc' instanceof String) //false
console.log(true instanceof Boolean) //false
console.log(Symbol() instanceof Symbol) //false

Every value in JavaScript besides null and undefined also has a constructor property storing the function that was used to construct it. This even works with primitives.


console.log([] instanceof Object, [] instanceof Array) //true true
console.log([].constructor === Object, [].constructor === Array) //false true
function isNumber(value) {
 //null.constructor and undefined.constructor throw an error when accessed
 if (value === null || value === undefined) return false
 return value.constructor === Number
}
console.log(isNumber(null), isNumber(undefined)) //false false
console.log(isNumber('abc'), isNumber([]), isNumber(() => 1)) //false false false
console.log(isNumber(0), isNumber(Number('10.1')), isNumber(NaN)) //true true true


Conclusion

In this page (written and validated by ) you learned about Javascript Datatypes . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript object type by constructor name.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.