Understanding Data Types
Photo By Joan Gamell on Unsplash
Understanding data types refers to a data structure that exists in a variable or represents a value. JavaScript is a dynamic programming language in terms of data types because variables in JavaScript change their data type depending on the value assigned to them.
Observe the following code :
let foo = 'string'; // tipe data saat ini string foo = 23; // sekarang menjadi tipe data number foo = true; // sekarang berubah menjadi tipe data boolean
Each programming language has its own data structure. JavaScript classifies data types into two categories: primitive data types and object data types.
Primitive data types are immutable, meaning that values in this data type cannot be changed or manipulated. There are seven types of primitive data types :
1. Boolean
The Boolean data type represents a logical entity and has only two conditions true and false. Pay attention to the following code.
const foo = true // boolean dengan kondisi true const foo2 = false // boolean dengan kondisi false
2. String Literal
A string or string literal is a data type that represents textual data. This type of data is written using single quotes (‘) or double quotes (“).
// menggunakan double quote const alamat = "rumah saya no 40" // atau dapat menggunakan single quote const kota = 'pontianak'
Tentunya terdapat perbedaan dari penulisan singgle quote dengan double quote. Dengan penulisan menggunakan singgle quote anda dapat menggunakan backslash (\) untuk menampilkan karakter tertentu. Seperti jika anda ingin menuliskan petik tunggal (singgle quote) pada value string anda.
const name = 'I\'m Muhammad Iqbal'; // output -> I'm Muhammad Iqbal
Tipe data string dapat dilakukan penggabungan terhadap nilai-nilai string lainnya. penggabungan tersebut dapat diimplementasikan dengan menggunakan concatenation operator (+) terhadap string yang ingin digabungkan.
const name = 'I\'m Muhammad Iqbal' + ' and i live in pontianak'; // output -> I'm Muhammad Iqbal and i live in pontianak
3. Number
If the value is a number, JavaScript considers it as a number data type, whether it is a decimal (integer) or a floating-point number (float).
// number untuk integer const angka = 121 // number juga bisa menjadi float const angkaPecahan = 7.5
4. Bigint
As the name suggests, BigInt stands for big integer numbers. It is a precision integer capable of representing numbers larger than 2^53 – 1.
If we rely only on the number data type, the maximum safe value is 9007199254740992. If a number exceeds this, the integer is no longer supported.
const limit = Number.MAX_SAFE_INTEGER; // output -> 9007199254740991 limit + 1 // output -> 9007199254740992 limit + 2 // output -> 9007199254740992 <-- mengunci nilai Number.MAX_SAFE_INTEGER + 1 /* Seharusnya Number.MAX_SAFE_INTEGER = 9007199254740991 + 2 = 9007199254740993 Akan tetapi otuput yang dihasilkan tetap 9007199254740992. Hal ini disebabkan karena melewati batas maximal Number.MAX_SAFE_INTEGER + 1 */
To easily check this, we can use Number.MAX_SAFE_INTEGER, which returns the maximum safe integer value, 9007199254740991.
If a number exceeds 9007199254740992, JavaScript locks the value at Number.MAX_SAFE_INTEGER + 1. If you try to increase it further, the system will still display 9007199254740992 due to its limitations.
To handle this, we need to use the BigInt() function. A BigInt can be created using the BigInt() function or by appending an n at the end of a value.
// cara pertama deklarasi tipe data bigint const bilangBesar = 9007199254740991n; // cara kedua deklarasi tipedata bigint const bilanganBesarJuga = BigInt(9007199254740991); // batas maksimal int const maksimal = BigInt(Number.MAX_SAFE_INTEGER); // batas maksimal di kali 2 const maksimalDikali2 = maksimal * 2n; // mengetahui tipe data console.log(typeof bilangBesar); // output -> bigint //hasil perkalian 2 batas maksimal (9007199254740991n * 2n) console.log(maksimalDikali2); // output -> 18014398509481982n
5. Symbol
The symbol data type was introduced in ES6. Symbol is a primitive data type that is immutable (cannot be changed). Each symbol data type returns a Symbol() value because it is created using the Symbol() function.
Because the purpose of using the Symbol function is to identify properties in an object, as the symbol data type is unique.
// tipe data symbol akan mengembalikan data symbol() const symbol1 = Symbol(); // output -> Symbol() // deklarasi dengan dua nilai symbol() yang sama const symbol2 = Symbol(23); const symbol3 = Symbol(23); console.log(symbol2 === symbol3); // output -> false // meskipun nilai kedua variable sama // akan tetapi tetap menghasilkan nilai false // karena fungsi symbol bersifat unik // berarti symbol akan tetap menganggap kedua variable tersebut berbeda
Symbol Syntax
The description is optional and is used for debugging, as we cannot access the value of a symbol.
A distinguishing feature of Symbol is that it cannot be converted into a string. Although almost all data types implicitly support conversion to a string using the alert() function, Symbol cannot do so.
const symbol1 = Symbol("sym"); alert(symbol1); // output -> TypeError: Cannot convert a Symbol value to a string
If you still want to display a symbol as a string, you must use the toString() function or symbol.description to retrieve the description of the symbol.
const symbol1 = Symbol("sym"); alert(symbol1.toString()); // output -> Symbol("sym") alert(symbol1.description); // output -> sym
6. Null
Null represents an empty value or the absence of a value. This is different from defining a variable with an empty string, as JavaScript will still consider that variable to have a string value.
Assigning a null value is done intentionally to define a variable without a value or as empty.
const foo = null; console.log(foo); // output -> null // ini merupakan kesalahan saat pengembangan awal javascript console.log(typeof null); // output -> object
The last line of code shows that null is of type object. This is a legacy mistake from JavaScript’s early development. Although I believe null is rarely used in production, it is still important to understand this data type.
7. Undefined
Simply put, undefined refers to a variable that has not been defined or assigned a value. Observe the following code :
const foo console.log(foo); // output -> undefined console.log(typeof foo); // output -> undefined
Undefined usually occurs due to negligence or mistakes made by the programmer when writing code. It can also be explicitly assigned, as shown below.
const foo = undefined; console.log(foo); // output -> undefined
However, it is best to avoid doing this in your code, as I believe it serves no real purpose.
The object data type is a mutable data type, meaning it can be manipulated using the methods provided by this data type. The classification of object data types is divided into three: Function, Array, and Object.
Since the discussion on functions, arrays, and objects is very important and quite extensive, we will cover it separately in a dedicated chapter.
That’s the explanation of Understanding Data Types that Mangcoding can share. Hopefully, this article is useful and provides new insights for you. If you have constructive feedback or suggestions, feel free to leave a comment or contact us via email and Mangcoding’s social media.
Source : Muhammadiqbal