| by Arround The Web | No comments

Explain JavaScript Records?

JavaScript comes with the non-primitive data type “Object” which is derived with the help of primitive(built-in) data types. The “Object” acts as an instance to access the JavaScript members. It is used to invoke the JavaScript function to perform the specific task that the primitive data types cannot. However, one disadvantage of this data type is that it performs the comparison operation on the basis of its identity, not the content. To resolve this problem JavaScript offers the new data type “Records” as it strictly compares on the basis of its contents, not identity.

This guide explains the JavaScript Records.

What are the “Records”?

JavaScript “Records” is a new primitive type(strings, numbers, symbols) similar to the built-in JavaScript Objects. The only difference is that “Records” are purely immutable i.e. the value of their keys cannot be changed once they have been initialized.

Syntax

The syntax of “Record” is identical to an “Object”, but it requires a “#(hash)” symbol before the curly braces that denote it as “Record”:

const recoredName = #{
 /*
 key: value
/*
}

 
Let’s use the above-stated syntax to create a new record.

How to Create Records?

To create a record, specify the “#(hash)” symbol at the beginning of the curly braces as shown in the below code block:

const person = #{
 fname: "Ali",
 lname: "Usman",
 age: 21,
}
console.log(person.fname)
console.log(person.lname)
console.log(person.age)

 
In the above code block:

    • The “person” refers to a new “Record” having the following keys “fname”, “lname”, and “age”.
    • Next, the “console.log()” method displays the “person” key values one by one respectively.

Note: The user can also specify the “Records” content in one line like this:

const person = #{fname: "Ali", lname: "Usman", age: 21}

 
Output


It can be seen that the output displays all the key values of the created Record “person”.

Limitation of Records

The “Record” does not accept an “Array” and an “Object” as its key. If the user passes them to a Record then the compiler generates a “TypeError”. The following code block shows it practically:

const newRecord = #{
 arr: ['HTML', 'CSS', 'JavaScript']
}
console.log(person.arr)

 
In the above code lines:

    • The “newRecord” initializes an array named “arr” as its key.
    • Next, the “console.log()” displays the “arr” key value specified in the “newRecord”.

Output


The console displays the “TypeError(passing un-expected type)” because the “Records” does not accept an array as a key.

Understanding JavaScript Records Using Examples

This section comprises the uses of “Records” practically with the help of given examples.

Let’s start with the first example.

Example 1: Records are Deeply Immutable

JavaScript “Records” are deeply immutable primitive types. The “deeply immutable” means all the key values of a Record cannot be modified or changed at any level once they have been set. The “primitive” types denote all the basic JavaScript data types such as string, number, null, undefined, and many others.

The following code block shows the stated concept practically:

const myRecord = #{
 name: "Ali",
 age: 21,
 }
myRecord.name= "Haroon"

 
In the above code block, the “myRecord” key “name” value is modified after its initialization.

Output


It can be observed that the compiler shows the “TypeError” on modifying the “newRecord” key value.

Example 2: Records Are Comparative

The main advantage of “Records” is that they are compared on the basis of their values, not identity. Whereas the “Objects” compare according to their identities, not values. If two Records values are equal then the compiler retrieves true.

Let’s see it practically with the help of given lines of code:

const myRecord = #{
 name: "Ali",
 age: 21,
 }
 console.log(myRecord === #{
 name: "Ali",
 age: 21,
 });

 
Here, the above code snippet creates two Records that are compared with the help of the “strict equality(===)” operator.

Output


The output returns a “true” Boolean value which means the specified operators i.e. “Records” are equal.

Example 3: Convert Record to Object

JavaScript “Records” can also be converted into an “Object” data type with the help of the “Object” constructor. Here is its practical implementation:

let myRecord = #{ One: 1, Two: 2 }
console.log(Object(myRecord))
console.log(typeof myRecord)

 
In the above lines of code:

    • The first “lconsole.og()” method uses the “Object” constructor to convert the “myRecord” into an “object.
    • The second “console.log()” method utilizes the “typeof” keyword to check the type of “myRecord”.

Output


The output shows the converted “newRecord” as an “Object” type which confirms that the “newRecord” has successfully converted to an “object”.

Example 4: Convert Object To Record

The user can also convert “Object” to “Record” for comparison purposes with the help of the “Record()” method. Let’s do it practically:

let myObj= { One: 1, Two: 2 }
let myRecord = Record(myObj)
console.log(myRecord)

 
Now, the above code snippet uses the “Record()” method to convert the given “myObj” object to “myRecord”.

Output


The output shows the converted object “myObj” into “myRecord” content successfully.

Example 5: Create New Records From Existing Records

As discussed in the first example, “Records” are immutable i.e. their key values cannot be modified. However, the user can create a new “Record” from the existing “Record” alongside the addition of some other values.

Follow the given code snippet to create a new record from the existing one:

let oldRecord = #{A: 1, B: 2 };
let newRecord = #{ ...myRecord, C: 3, D:4}
console.log(newRecord)

 
In the above JavaScript code:

    • The “oldRecord” refers to an existing record having two key values.
    • Next the “newRecord” corresponds to a new Record that is created with the help of the existing “oldRecord” and also by adding the new specified key values.
    • Lastly, the “console.log()” displays the newly created Record named “newRecord”.

Output


The output displays all the key values of the newly created Record.

Example 6: Access Records Using “Object.keys()” Method

The user can use the built-in JavaScript “keys()” method of “Object” to access the keys of a Record. In this scenario, it is used to access the “myRecord” keys:

let myRecord = #{A: 1, B: 2 };
let recordKeys = Object.keys(myRecord);
console.log(recordKeys)

 
The above code snippet uses the “Object.keys()” method to access all the keys present in the “myRecord”.

Output


The output shows all the keys of the “myRecord” in the array format and also with their indexes in a key-value pair format.

Conclusion

JavaScript “Records” are the advanced level data type that is deeply immutable. It works similarly to an “Object” but the main difference is that its value can be changed or updated once it has been set. It requires a “#(hash)” symbol before the curly braces for the declaration otherwise it acts as an object. This guide briefly explained the JavaScript Records data type.

Share Button

Source: linuxhint.com

Leave a Reply