IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesJavaScriptOperators  Print This     

Cross Ref > Operators

By Mike Prestwood

JavaScript versus Delphi Prism: A side by side comparison between JavaScript and Delphi Prism.

 
Operators
 

A language symbol used for assignment, comparison, computational, or as a logical.

Assignment

[Other Languages] 

Languages Focus

Common assignment operators for languages include =, ==, and :=. An assignment operator allows you to assign a value to a variable. The value can be a literal value like "Mike" or 42 or the value stored in another variable or returned by a function.

JavaScript:   =

JavaScript uses = for it's assignment operator.

Syntax Example:
//Regular assignment.
LastName = 'Spitz';
  
//Variable declaration with assignment.
var FullName = 'Randy Spitz';
var Age = 38;
Delphi Prism:   :=

Same as Delphi.

Syntax Example:
FullName: String;
Age: Integer;
  
FullName := "Randy Spitz";
Age := 38;




Comparison Operators

[Other Languages] 

General Info: Round Floating Point Numbers

When comparing floating point numbers, make sure you round to an acceptable level of rounding for the type of application you are using.

Languages Focus

A comparison operator compares two values either literals as in "Hello" and 3 or variables as in X and Counter. Most languages use the same operators for comparing both numbers and strings. Perl, for example, uses separate sets of comparison operators for numbers and strings.

JavaScript:   ==, !=

Common comparison operators:

== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

Syntax Example:
//Does JavaScript evaluate the math correctly? No!
if (.1 + .1 + .1 == .3) {
document.write("correct");
}
else {
document.write("not correct");
}
Delphi Prism:   =, <>

Same as Delphi. Common comparison operators:

= equal
<> not equal
< less than
> greater than
<= less than or equal
>= greater than or equal
Syntax Example:
//Does Prism evaluate the math correctly? No!
//This is different than later versions of 
//Delphi that muse MaxSingle in math.pas.
If .1 + .1 + .1 = .3 Then
MessageBox.Show("correct")
Else
MessageBox.Show("not correct");




Empty String Check

[Other Languages] 

Languages Focus

An empty string is a zero length string, a string that is equal to null (""), or not assigned. In some languages, you can check if a string is empty by comparing it to an empty string (""). Some languages distinguish between nil and null ("") so checking if the length is 0 is easier.

[Not specified yet. Coming...]
Delphi Prism:   length

In Prism, a string can be nil (unassigned), assigned an empty string (""), or assigned a value.  Therefore, to check if a string is empty, you have to check against both nil and (""). Alternatively, you can check the length of the string or use String.IsNullOrEmpty.

Syntax Example:
var s: String; 
 
if (s = nil) or (s = '') then
  MessageBox.Show("empty string");

or use length:

if length(s) = 0 then
  MessageBox.Show("empty string");




Logical Operators

[Other Languages] 

Languages Focus

Logical operators perform conditional and, or, and not operations. Some languages support both binary logical operators that link two and unary logical operators negate (make opposite) the truth value of its argument. Finally, some languages short circuit logic. For example, with this or that, if this is an expression returning true, then that is never executed.

JavaScript: 

JavaScript logical operators:

&& and, as in this and that
|| or, as in this or that
! Not, as in Not This

JavaScript always short circuits. Given the expression this || that, if this evaluates to true, then that is never executed.

Syntax Example:
//Given expressions a, b, c, and d:
if !((a && b) && (c || d)) {
  //Do something.
}
Delphi Prism: 

Prism logical operators:

and and, as in this and that
or or, as in this or that
not Not, as in Not This
xor either or, as in this or that but not both

Syntax Example:  
//Given expressions a, b, c, and d:
if Not (a and b) and (c or d) then
  //Do something.




String Concatenation

[Other Languages] 
JavaScript:  "String Concatenation" +

To concatenate two strings, a string to an integer, or a string to a floating point number, use the + operator. JavaScript performs implicit casting when concatenating a string and a number. For example, to convert a floating point number to a string just concatenate an empty string to the number as in "" + 3.2.

Syntax Example:
 
// -->
Delphi Prism:   +

Unlike Delphi, Prism performs implicit casting. To concatenate two strings, a string to an integer, or a string to a floating point number, use the + operator. For example, to convert a floating point number to a string just concatenate an empty string to the number as in "" + 3.2.

Alternatively, you can use the System.Text.StringBuilder class which frequently but not always provides faster code.

Syntax Example:
var FirstName : String;
var LastName : String;
  
FirstName := 'Mike';
LastName := 'Prestwood';
ShowMessage('Full name: ' + FirstName + ' ' + LastName);
  
//Implicit casting of numbers.
//
//This fails:
//MessageBox.Show(3.3);
//
//This works:
MessageBox.Show("" + 3.3);




Unary Operators

[Other Languages] 

General Info: Unary Operator

An operation with only one operand (a single input). Common unary operators include + plus, - minus, and bitwise not. Some operators can function as both unary and binary operators. For example, + and - operators can serve as either.

Languages Focus

What unary operators are supported in additoin to the standard plus, minus, and bitwise not.

JavaScript: 

An operation with only one operand (a single input). JavaScript unary operators include ++ and --. They can be used either before or after a variable as in: a++, b--, and ++a, and --b.

Examples:

iCounter++;
iCounter--;
 
++iCounter;
--iCounter;
Syntax Example:
var iCounter=0;
 
for (iCounter=0;iCounter<=5;iCounter++)
{
document.write("Count is " + iCounter + "<br>");
}
Delphi Prism: 

The obvious Prism unary operators are +, -, and Not.

+ Plus
- Minus
Not Bitwise Not
Inc() Increment
Dec() Decrement

Syntax Example:
var i: Integer := 1;
Inc(i);
MessageBox.Show("" + i);  //Displays 2




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]