dsabyte

Fri Nov 05 2021 (1 year ago)

Author of the @dsabyte

We are a community of software engineers.

Data types and it's applications in JavaScript

Almost all programming language have built in data types and most of the modern programming languages also support user defined data types. In this article, i'll aim to attempt a quick overview of the data types.

Type of data types in Javacript

There are two types of data types in JavaScript

  1. Primitive - Also called built in types which are immutable

  2. Objects

Primitive types with examples

Primitive data types are the data types which are defined by the programming language itself.

Boolean

Boolean data type represent truthly values, which can be either true or false

Example

const isIndiaGreat = true;
const IsIndiaNew = false;

// uses
if(isIndiaGreat){
    console.log("The India is a great country!");
}else{
    console.log("The India is a great country anyway!");
}

Null Type

Null type has exactly one value, null. null represents nothing, which means when you need a variable which can indicate absence of something, then you can assign it to null.

Example

const value = null;

// uses
if(value){
    // do something..
}

Undefined Type

A variable which is declared but it has not assigned any value is undefined.

Example

let doctor;

console.log(doctor); // undefined

Number Type

ECMAScript has two built in Number types: 1:) Number, 2:) BigInt.

The Number type is 6464 bit binary format double precision number IEEE 754 value.

Range of Number

Range of Number type is (2531)to(2531)-(2^{53}-1) \medspace to \medspace (2^{53}-1) .

Symbolic values in Number types

In addition to the range in Number type, it has three symbolic types.

  1. + Infinity

  2. - Infinity

  3. NaN - Not a Number

Constants values in Number type

  1. MAX_VALUE - 1.7976931348623157e+3081.7976931348623157e+308

  2. MIN_VALUE - 5e3245e-324

  3. MAX_SAFE_INTEGER - 90071992547409919007199254740991

  4. MIN_SAFE_INTEGER - 9007199254740991-9007199254740991

BitInt Type

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.

Example

const x = 2n ** 53n;
// 9007199254740992n
const y = x + 1n;
// 9007199254740993n

String Type

JavaScript's String type is used to represent textual data. It is a set of "elements" of 16-bit unsigned integer values. Each element in the String occupies a position in the String. The first element is at index 0, the next at index 1, and so on. The length of a String is the number of elements in it.

With conventions, it is possible to represent any data structure in a string. This does not make it a good idea. For instance, with a separator, one could emulate a list (while a JavaScript array would be more suitable). Unfortunately, when the separator is used in one of the "list" elements, then, the list is broken. An escape character can be chosen, etc. All of this requires conventions and creates an unnecessary maintenance burden.

Use strings for textual data. When representing complex data, parse strings, and use the appropriate abstraction.

Example

const str = "@dsabyte";
console.log(str); // @dsabyte

Symbol Type

A Symbol is a unique and immutable primitive value and may be used as the key of an Object property (see below). In some programming languages, Symbols are called "atoms".

For more details see Symbol and Symbol object wrapper in JavaScript.

References

MDN Docs

JavaScript

© 2021 dsabyte. All rights reserved