Languages
[Edit]
EN

JavaScript - data types

0 points
Created by:
elmer
646

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 have true/false value.
  • null for unknown values.
  • undefined for unassigned values.
  • object - key-value pairs of collection of data.
  • symbol for unique identifiers.

 

1. The typeof operator

// ONLINE-RUNNER:browser;

console.log(  typeof('sample text') ); // string
console.log(  typeof( 0           ) ); // number
console.log(  typeof( 10n         ) ); // bigint
console.log(  typeof( true        ) ); // boolean
console.log(  typeof( null        ) ); // object
console.log(  typeof( undefined   ) ); // undefined
console.log(  typeof( Symbol('example_id'))); // symbol
console.log(  typeof( Math        ) ); // object
console.log(  typeof( Date        ) ); // function

 More complex examples

// ONLINE-RUNNER:browser;

function Student() {
	// class logic...
}

console.log(typeof(  null                      )); // object
console.log(typeof(  undefined                 )); // -----> undefined
                                                       
console.log(typeof(  { }                       )); // object
console.log(typeof(  new Object()              )); // object
console.log(typeof(  new Student()             )); // object
console.log(typeof(  new function() { }        )); // object

console.log(typeof(  [ ]                       )); // object
console.log(typeof(  new Array()               )); // object
                                                       
console.log(typeof(  true                      )); // -----> boolean
console.log(typeof(  Boolean(true)             )); // -----> boolean
console.log(typeof(  new Boolean(true)         )); // object

console.log(typeof(  5                         )); // -----> number
console.log(typeof(  Number(5)                 )); // -----> number
console.log(typeof(  new Number(5)             )); // object

console.log(typeof(  5n                        )); // -----> bigint
console.log(typeof(  BigInt(5)                 )); // -----> bigint

console.log(typeof(  'a'                       )); // -----> string
console.log(typeof(  String('a')               )); // -----> string
console.log(typeof(  new String('a')           )); // object
                                                       
console.log(typeof(  function() { }            )); // function
console.log(typeof(  Date                      )); // function
console.log(typeof(  RegExp                    )); // function
console.log(typeof(  Function                  )); // function
                                                       
console.log(typeof(  new Function('')          )); // function
console.log(typeof(  new Date()                )); // object
console.log(typeof(  /[a-z]/g                  )); // object
console.log(typeof(  new RegExp('[a-z]', 'g')  )); // object

Note: simple types and undefined were marked in comments with ----->

Comparing the result of typeof, toString and instanceof

ENTRY                       typeof        toString      instanceof
     
null                        object        Null          false  (for Object)
undefined                   undefined     Undefined     false  (for Object)

{ }                         object        Object        true   (for Object)
new Object()                object        Object        true   (for Object)
new Student()               object        Object        true   (for Student) // custom class used
new function() { }          object        Object        true   (for Object)

[ ]                         object        Array         true   (for Array)
new Array()                 object        Array         true   (for Array)

true                        boolean       Boolean       false  (for Boolean) 
Boolean(true)               boolean       Boolean       false  (for Boolean)
new Boolean(true)           object        Boolean       true   (for Boolean)

5                           number        Number        false  (for Number)
Number(5)                   number        Number        false  (for Number)
new Number(5)               object        Number        true   (for Number)

5n                          bigint        BigInt        false  (for BigInt)
BigInt(5)                   bigint        BigInt        false  (for BigInt)

'a'                         string        String        false  (for String)
String('a')                 string        String        false  (for String)
new String('a')             object        String        true   (for String)

function() { }              function      Function      true   (for Function)
Date                        function      Function      true   (for Function)
RegExp                      function      Function      true   (for Function)
Function                    function      Function      true   (for Function)

new Function('')            function      Function      true   (for Function)
new Date()                  object        Date          true   (for Date)
/[a-z]/g                    object        RegExp        true   (for RegExp)
new RegExp('[a-z]', 'g')    object        RegExp        true   (for RegExp) 

References

  1. JavaScript - how to check type of object? 
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript tutorial

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join