Hire a freelance web designer in Singapore for your next website design project with our guide to the four crucial website pages in 2024. Optimize your website success with expert insights.
You Might Not Be Familiar with These Things about the JavaScript Array – 1
Every programming element is important. However, the array is special. It enables you to use a single variable to refer the storage of multiple variables. With an array, it has become much easier to handle multiple values. There are some limitations of this referenced data type. This is the reason why Java, C#, and other high-level languages rely on objects to store values of different primitive data types. However, still, arrays are very important element of middle and high-level programming languages.
Almost all programming languages have common properties for an array. In this article, we are going to discuss one of the three important things you probably do not know about JavaScript arrays.
Adding Custom Properties to Array
There are three types of properties that you can add to a JavaScript array.
Indices of an array
Predefined or built-in properties
Custom properties
Every beginner developer is familiar with the first two properties. Experienced developers regularly use all three properties.
Indices as Properties
In JavaScript, the syntax of creating an array is:
var ary = [“eyes”,”hands”,”ears”];
Indices are properties of array elements and are always positive integers. Just as we have a key-value pair in an object, we have an index-element pair in JavaScript array. Indices are different from other built-in properties of an array. A developer can set an element in the index with bracket syntax.
For example
ary[3] =”xyz”;
Built-In Properties
There are some properties that are frequently used by developers. Therefore, these properties are predefined in JavaScript library. One of the examples of built-in properties is length. These properties are also used to customize generic objects for different needs.
There are two ways for accessing built-in properties:
object.key
or, ary[“key”]
In var ary = [“eyes”,”hands”,”ears”];
ary.length will return 3 and ary[“length”] will also return 3.
Adding Custom Properties
In JavaScript array, usually, a developer needs not to create and add a custom property. This is the reason why beginners are not taught how they can create and add custom properties to a JavaScript array. However, a developer may encounter a special case in which he has to define and add a custom property.
In the following example, we have created a property to identify the class of an array.
Var ary=[“eyes”,”hands”,”ears”];
ary.itemClass =”bodyParts”;
console.log(ary+ ”are” +ary.itemClass);
The output will be “eyes, hands, ears are bodyParts”
Every program written in JavaScript or any other programming languages consists of at least one array. If you are working as a freelance website developer in Singapore, you know how important arrays are. Even if you are an experienced developer, there is still more to explore in JavaScript.