1// get type of variable
2
3var number = 1
4var string = 'hello world'
5var dict = {a: 1, b: 2, c: 3}
6
7console.log(typeof number) // number
8console.log(typeof string) // string
9console.log(typeof dict) // object
1> typeof "foo"
2"string"
3> typeof true
4"boolean"
5> typeof 42
6"number"
7
8if(typeof bar === 'number') {
9 //whatever
10}
1//typeof() will return the type of value in its parameters.
2//some examples of types: undefined, NaN, number, string, object, array
3
4//example of a practical usage
5if (typeof(value) !== "undefined") {//also, make sure that the type name is a string
6 //execute code
7}
1console.log(typeof 93);
2// Output = "number"
3
4console.log(typeof 'Maximum');
5// Output = 'string'
6
7console.log(typeof false);
8// Output = "boolean"
9
10console.log(typeof anUndeclaredVariable);
11// Output = "undefined"
1exports.is = (data) => {
2const isArray = Array.isArray(data) && 'array'
3const isObject = data == {} && 'object'
4const isNull = data == null && 'null'
5
6const isGrouping = isArray || isObject || isNull
7const isCheck = !isGrouping ? typeof data : isGrouping
8
9const isTypeData = ['number','string','array','symbol','object','undefined','null','function', 'boolean']
10const isMatch = isTypeData.indexOf(isCheck)
11const isResult = isTypeData[isMatch]
12return isResult
13}