-
Notifications
You must be signed in to change notification settings - Fork 852
/
check-data-type-with-typeof.js
74 lines (53 loc) · 2.79 KB
/
check-data-type-with-typeof.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
console.log(typeof 42) // expected output: "number"
console.log(typeof "blubber") // expected output: "string"
console.log(typeof true) // expected output: "boolean"
console.log(typeof undeclaredVariable) // => "undefined";
console.log(typeof {}) // object
console.log(typeof []) // object
console.log(typeof "") // object
console.log(typeof typeof); // Would not compile giving me SyntaxError: Unexpected token
// Undefined
console.log(typeof something); // Undefined
typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined';
// Symbols
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'
console.log(Symbol("prop")) // Symbol(prop)
// Numbres
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof(42) === 'number';
console.log(typeof NaN); // => number
typeof '1' === 'string'; // note that a number within a string is still typeof string
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // Despite being "Not-A-Number"
typeof Number('1') === 'number'; // Number tries to parse things into numbers
typeof Number('shoe') === 'number'; // including values that cannot be type coerced to a number
// Bigint
typeof 42n === 'bigint';
/* BigInt is a built-in object that provides a way to represent whole numbers larger than 2^(53) - 1, which is the largest number JavaScript can reliably represent with the Number primitive and represented by the Number.MAX_SAFE_INTEGER constant. BigInt can be used for arbitrarily large integers.
The BigInt type is a numeric primitive in JavaScript that can represent integers with arbitrary precision. With BigInts, you can safely store and operate on large integers even beyond the safe integer limit for Numbers.
A BigInt is created by appending n to the end of an integer or by calling the constructor.
*/
// Function
const func = () => console.log('Its a function');
console.log(typeof func); // function
typeof class C {} === 'function';
typeof Math.sin === 'function';
// Boolean
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(1) === 'boolean'; // Boolean() will convert values based on if they're truthy or falsy
typeof !!(1) === 'boolean'; // two calls of the ! (logical NOT) operator are equivalent to Boolean()
typeof new Date() === 'object';
typeof /regex/ === 'object'; // See Regular expressions section for historical results
// The following are confusing, dangerous, and wasteful. Avoid them.
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String('abc') === 'object';
### Further Reading
[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)