dsabyte
Fri Nov 05 2021 (1 year ago)
Author of the @dsabyte
We are a community of software engineers.
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.
There are two types of data types in JavaScript
Primitive - Also called built in types which are immutable
Objects
Primitive data types are the data types which are defined by the programming language itself.
Boolean data type represent truthly values, which can be either true
or false
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 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
.
const value = null;
// uses
if(value){
// do something..
}
A variable which is declared but it has not assigned any value is undefined
.
let doctor;
console.log(doctor); // undefined
ECMAScript has two built in Number
types: 1:) Number, 2:) BigInt.
The Number
type is bit binary format double precision number IEEE 754 value.
Number
Range of Number
type is .
Number
typesIn addition to the range in Number
type, it has three symbolic types.
+ Infinity
- Infinity
NaN - Not a Number
Number
typeMAX_VALUE -
MIN_VALUE -
MAX_SAFE_INTEGER -
MIN_SAFE_INTEGER -
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.
const x = 2n ** 53n;
// 9007199254740992n
const y = x + 1n;
// 9007199254740993n
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.
const str = "@dsabyte";
console.log(str); // @dsabyte
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.
© 2021 dsabyte. All rights reserved