EN
JavaScript - data types
0 points
In this article, we'll show you commonly used data types in JavaScript.
There are eight basic data types:
string
for strings (zero or more characters).number
for integers or floating points.bigint
is for integer numbers of arbitrary length.boolean
- only havetrue
/false
value.null
for unknown values.undefined
for unassigned values.object
- key-value pairs of collection of data.symbol
for unique identifiers.
xxxxxxxxxx
1
console.log( typeof('sample text') ); // string
2
console.log( typeof( 0 ) ); // number
3
console.log( typeof( 10n ) ); // bigint
4
console.log( typeof( true ) ); // boolean
5
console.log( typeof( null ) ); // object
6
console.log( typeof( undefined ) ); // undefined
7
console.log( typeof( Symbol('example_id'))); // symbol
8
console.log( typeof( Math ) ); // object
9
console.log( typeof( Date ) ); // function
xxxxxxxxxx
1
function Student() {
2
// class logic...
3
}
4
5
console.log(typeof( null )); // object
6
console.log(typeof( undefined )); // -----> undefined
7
8
console.log(typeof( { } )); // object
9
console.log(typeof( new Object() )); // object
10
console.log(typeof( new Student() )); // object
11
console.log(typeof( new function() { } )); // object
12
13
console.log(typeof( [ ] )); // object
14
console.log(typeof( new Array() )); // object
15
16
console.log(typeof( true )); // -----> boolean
17
console.log(typeof( Boolean(true) )); // -----> boolean
18
console.log(typeof( new Boolean(true) )); // object
19
20
console.log(typeof( 5 )); // -----> number
21
console.log(typeof( Number(5) )); // -----> number
22
console.log(typeof( new Number(5) )); // object
23
24
console.log(typeof( 5n )); // -----> bigint
25
console.log(typeof( BigInt(5) )); // -----> bigint
26
27
console.log(typeof( 'a' )); // -----> string
28
console.log(typeof( String('a') )); // -----> string
29
console.log(typeof( new String('a') )); // object
30
31
console.log(typeof( function() { } )); // function
32
console.log(typeof( Date )); // function
33
console.log(typeof( RegExp )); // function
34
console.log(typeof( Function )); // function
35
36
console.log(typeof( new Function('') )); // function
37
console.log(typeof( new Date() )); // object
38
console.log(typeof( /[a-z]/g )); // object
39
console.log(typeof( new RegExp('[a-z]', 'g') )); // object
Note: simple types and
undefined
were marked in comments with----->
.
xxxxxxxxxx
1
ENTRY typeof toString instanceof
2
3
null object Null false (for Object)
4
undefined undefined Undefined false (for Object)
5
6
{ } object Object true (for Object)
7
new Object() object Object true (for Object)
8
new Student() object Object true (for Student) // custom class used
9
new function() { } object Object true (for Object)
10
11
[ ] object Array true (for Array)
12
new Array() object Array true (for Array)
13
14
true boolean Boolean false (for Boolean)
15
Boolean(true) boolean Boolean false (for Boolean)
16
new Boolean(true) object Boolean true (for Boolean)
17
18
5 number Number false (for Number)
19
Number(5) number Number false (for Number)
20
new Number(5) object Number true (for Number)
21
22
5n bigint BigInt false (for BigInt)
23
BigInt(5) bigint BigInt false (for BigInt)
24
25
'a' string String false (for String)
26
String('a') string String false (for String)
27
new String('a') object String true (for String)
28
29
function() { } function Function true (for Function)
30
Date function Function true (for Function)
31
RegExp function Function true (for Function)
32
Function function Function true (for Function)
33
34
new Function('') function Function true (for Function)
35
new Date() object Date true (for Date)
36
/[a-z]/g object RegExp true (for RegExp)
37
new RegExp('[a-z]', 'g') object RegExp true (for RegExp)