Javascript: Data Types and Structures
Types are different variations of data that can be stored and manipulated within a program. Every programming language has its own built-in data types and structures that differ from one to another.
According to the latest ECMAScript standard, there are nine data types in Javascript.
The Primitive Types
- Number
- String
- BigInt
- Symbol
- Boolean
- Undefined
- Null
and, The Structural Types
- Object
- Function
Checking the Type
The typeof operator in javascript returns a string that indicates the type of a variable.
const typeChecking = "This is a string"console.log(typeof typeChecking) // output: string
Number
Represents a data type that is an integer, float, hexadecimal, octal or exponential value.
In Javascript, a Number is a double-precision 64-bit binary format IEEE 754 value, A number in javascript can store up to 17 decimal places of precision. The largest value it can hold is about 1.8×10³⁰⁸. Anything beyond that is replaced with the special number constant Infinity.
const number = 99const bigNumber = 1.8 * Math.pow(10, 307)const infinity = number * bigNumberconsole.log(number) // output: 99console.log(bigNumber) // output: 1.8e+307console.log(infinity) // output: Infinity
String
Represents a sequence of characters as text usually wrapped with “….” or ‘….’.
const string = "Storing a srting."
const anotherString = 'Storing another string'
BigInt
Big Integer, or Bigint, can be created by appending an n at the end of an integer or by calling the BigInt() function.
Although bigint values seem like a number, they differ from number types in some way. A bigint value can not be used with Javascript’s built-in Math object and can not be mixed with regular numbers.
const bigint = 999999nconst anotherBigint = BigInt(999999)console.log(typeof bigint) // output: bigintconsole.log(typeof anotherBigint) // // output: bigint
Symbol
Symbols are unique and immutable primitives that can be used as object keys. Javascript object key values can only either be a string or a symbol.
Javascript Symbol() constructor can create a symbol primitive that is guaranteed to be unique.
const sym = Symbol(1)const anotherSym = Symbol(1)console.log(sym === anotherSym) // output: false// Used as object keysconst user = [{
user1: "name1",
id: sym
},{
user2: "name2",
id: anotherSym
}]
Boolean
Booleans are logical entities that can be either true or false.
const a = 1const b = 2const c = 1console.log(a === b) // output: flaseconsole.log(a === c) // output: true
Undefined
undefined is a global object that represents the primitive value Undefined.
A variable that has no value assigned has the typeof undefined. A function returns undefined if nothing is being returned from it. Also, a method or statement can return undefined if a variable that has been evaluated contains no values.
let withoutValues;function returnsNothing () { const a = 1; const b = 2 a + b;};console.log(typeof withoutValues); // output: undefinedconsole.log(returnsNothing()); // output: undefined
Null
The value null is written with the literal null. It represents an intentional lack of identification of any object value. The difference between null and undefined is, null is not an identifier of a global object property. It simply indicates that the variable points to no object. Often used as an initial placeholder for a variable latter to be assigned.
let withoutValues;let withNull = null;console.log(typeof withoutValues); // output: undefinedconsole.log(typeof withNull); // output: object
Object
The object is one of Javascript’s structured data types that stores values in a {key: value} combination and is used for complex entries. An object in javascript can be declared using the Object() constructor or by using the object initializer syntax.
const user = { username: "name", id: 345, friends: ["user1", "user2"], email: "user@example.com"}console.log(typeof obj) // output: object
Function
A Function in javascript is similar to a procedure. 1st, You have to declare a function and write the necessary processing rules inside it. Then, upon calling, it calculates values passed into it as inputs and after the defined calculations it returns the output.
function addNums (a, b) { const c = 3 return a + b + c}console.log(addNums(1, 2)) // oupput: 6
So Where Does The Array Fits Into All These?
If you are exploring javascript basics and have written at least a little bit of code with it or have knowledge about basic data structures, you might wonder why array is not mentioned as a data structure type of javascript. Well, this is simply because Javascript only has Object and Function as structural types. Arrays are basically Objects with indexes as keys and elements as values underneath.
Like this,
const underneathAnArray = {
"0" : 1,
"1" : 2,
"2" : 4,
"3" : 7,
"4" : 8,
"5" : 0,
}
To learn more about Javascript data types and Arrays:
Data Types: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
Array: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array