What is the Generic and non generic collections?
1335
05-Oct-2019
Aryan Kumar
07-Jun-2023Generic collections are collections that can hold elements of any type. This is in contrast to non-generic collections, which can only hold elements of a specific type.
Generic collections are introduced in JavaScript ES2015. They are implemented using the class keyword.
Here is an example of a generic collection:
Code snippet
This class can be used to create a collection that can hold elements of any type. For example, you could create a collection of strings or a collection of numbers.
Non-generic collections are collections that can only hold elements of a specific type. This is the older way of creating collections in JavaScript.
Here is an example of a non-generic collection:
Code snippet
This code creates an array that can hold elements of any type. However, it is not type-safe. This means that you could accidentally add an element of the wrong type to the array.
Generic collections are preferred over non-generic collections because they are type-safe. This means that you can be sure that the elements in a generic collection are of the correct type. This can help to prevent errors and improve the performance of your code.
Here is a table of the differences between generic and non-generic collections:
It is important to note that generic collections are not supported by all browsers. If you need to support older browsers, you may need to use non-generic collections.
Shrikant Mishra
05-Oct-2019These are two types of collections one is generic collection another one is non-generic collection.
Generic Collection:
Into this, We can group one type of object in a single collection is called generic collection. Here we don't need to convert a particular type like string, int, etc.
Non Generic Collection:
Into this, We can group any type of object in a single collection is called non-generic collection. Here we need to convert a particular type like string, int,etc., otherwise, we will face issues in run time.