JavaScript List Differences

*Lists are copied and compared by value*

In JavaScript, lists are copied and compared by reference. In DScript, lists are a fundamental data type and are copied and compared by value, just like numbers and text strings.

Consider the following code example:

var x=[1,2,3,4,5];
var y=x;
y[0]=99;

In JavaScript, the statement y=x does not copy the entire contents of the list x into y. Instead, the value of y is just a pointer to x. This means after the statement y[0]=99 is executed, the value of x is [99,2,3,4,5], even though x is never explicitly modified. Compare this result to code using scalars:

var x=1;
var y=x;
y=99;

After this code executes, x has a value of 1, and y is 99.

In JavaScript, scalars are copied by value and lists are copied by reference. This is done to make list copying more efficient. However, DScript achieves the same efficiency while still copying all data (including lists) by value.

Consider the original example again. This time the example is run in DScript.

var x=[1,2,3,4,5];
var y=x;
y[0]=99;

The first statement, x=[1,2,3,4,5] behaves in exactly the same manner in both DScript and JavaScript. In JavaScript, the second statement, y=x, sets y equal to a pointer to x. This is also what DScript does.

Now, consider what happens in the last statement, y[0]=99. JavaScript modifies the list to which both x and y point. DScript, however, realizes x and y point to the same list; so, a copy of the list is made before the modification takes place. When the code is finished, the value of both x and y in JavaScript is [99,2,3,4,5]. In DScript, x is [1,2,3,4,5] and y is [99,2,3,4,5].

DScript keeps the efficiency of copying lists with pointers while using the pointers intelligently enough that the result is just as if the list was copied by value.

The by-reference versus by-value difference in the way DScript and JavaScript treat lists is also evident in comparisons. For example, the comparison statement x==y below evaluates to false in JavaScript, but it is true in DScript.

var x=[1,2,3,4,5];
var y=[1,2,3,4,5];
if(x==y)
    ...;

The differences here might seem subtle, but they are extremely important. In DScript, a list is a simple data type, like a number or a text string. Lists are copied and compared by value, just like all other data types. This feature, coupled with list and matrix processing, adds greatly to DScript's abilities. For example, if you want to add all elements in matrix B to the elements in matrix A, you would use JavaScript code like this:

for(var x=0; A[x][0]!=null; x++) {
  for(var y=0; A[x][y]!=null; y++)
    A[x][y]+=B[x][y];
}

Using DScript you can do the same thing with a simple statement like this:

A+=B;

In addition, the DScript code executes at about 30 times the speed of the equivalent JavaScript code.

In DScript, if you want to pass a list pointer to a function so the function can modify the original list, just define the function arguments with the @ character as in the following example:

Increment(@list):={
  list+=1;
}

var x=[1,2,3,4,5];
Increment(x);

Lists are not sparse

In JavaScript, lists are sparse, that is, you can assign a value to the 100th element in the list without the first 99 even existing. In DScript, you can assign a value to the 100th element only if the list contains at least 100 elements.