Array

JBS arrays are used to store multiple values in a single variable.

Example

let colors = ["red", "blue", "yellow"];

 

What is an Array?

An array is a special variable, which can hold more than one value at a time.

If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

 

let color1 = "red";
let color2 = "blue";
let color3 = "yellow";

 

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 color, but 20?

The solution is an array!

An array can hold many values under a single name, and you can access the values by referring to an index number.

 

Creating an Array

Using an array literal is the easiest way to create a JavaScript Array.

Syntax:

 

let  array_name = [item1, item2, ...];

 

Example

let colors = ["red", "blue", "yellow"];

 

Spaces and line breaks are not important. A declaration can span multiple lines:

 

let colors = [
  "red",
   "blue", 
  "yellow"];

 

Display complete Array

colors return the complete array as string with comma delimiter

let colors = ["red", "blue", "yellow"];
alert(colors); // return "red,blue,yellow"

 

Access the Elements of an Array

This statement display the value of the first element in colors

let colors = ["red", "blue", "yellow"];
alert(colors[0]);

 

Note: 0 is the first element, 1 is the second element

 

Changing an Array Element

This statement change the value of first element in colors

let colors = ["red", "blue", "yellow"];
colors[1]="black"; // change element 1 (second element) from blue to black
alert(colors); // return "red,black,yellow"

 

Store different type of values

An array can store all Variable and object types

let myArray=new Array(); // declare an empty array 
myArray[0]=Date; // Store current date as object "Date"
myArray[1]=Number(5); // Store Number object in Element 1
myArray[2]=[3,4,5] ; // Store array object in Element 2
myArray[3]=6; // Store number value 
myArray[4]={preName:"Martin",lastName:"Heigl"}; // Store object in Element 4
alert(myArray);

 

return:

Sun Sep 27 2020 08:33:28 (Company Time),5,3,4,5,6,{"preName":"Martin","lastName":"Heigl"}

 

Length Property

The lengh property return the count of entries inside the array

let colors = ["red", "blue", "yellow"];
alert(colors.length); // return 3

 

Loopping Arrray elements

Example: use for for - next

 

let n=[1,2,3,4];
for (i=0;i<n.length;i++)   n[i]=n[i] * 2;
alert(n) // return 2,4,6,8

 

or using "for ... in"

 

let n=[1,2,3,4];
for (i in n n[i]=n[i] * 2;
alert(n) // return 2,4,6,8

 

Add Array Element

Add the element after last entry. Use keyword length

let colors = ["red", "blue", "yellow"];
colors[colors.length] = "black";
alert(colors); // return red,blue,yellow,black

 

 

Associative Arrays

Many programming languages support arrays with named indexes. Arrays with named indexes are called associative arrays (or hashes). JBS does not support arrays with named indexes. For this use JBS objects

In JBS, arrays always use numbered indexes.

let person = [];
person[0] = "Martin";
person[1] = "Heigl";
person[2] = 46;
let x = person.length; // person.length will return 3
let y = person[0]; // person[0] will return "Martin"

 

 

 

Methods

Method

Description

toString()

Return Array as string. Delimiter is comma

toJson()

Return Array as Json String (not as Json object)

length()

Return count of elements

 

 

Note: this is current not supported

Current no additional Methods available

Follow not supported:

let colors = new Array("red", "blue", "yellow");