Comparison Table for Pascal and JavaScript

Language Feature Pascal JavaScript
Statement separator ; ; (or new line)
Comment

//Comment after code
{Comment spanning
lines}

//Comment after code
/* Comment spanning
lines */

Quoted string

'Hello World'

"Hello World" or 'Hello World'

Code block

begin
  lines of code
end;

{
  lines of code
}

Declaring constants const VAT_RATE = 0.175;

var VAT_RATE = 0.175;

(const VAT_RATE = 0.175 is not universally accepted according to stackoverflow).

Declaring variables

var
  forename, surname: string;
  paysTax: Boolean;
  totalHits: integer;
   averageScore: double;
             //same as real
  initial: char;
        //same as ansiChar  
  months: array [1..12] of string;
  rainfall: array [2004..2010, 1..12] of integer;
  ageRecords: array[1..1000] of TRec;

Also  wideChar,  byte,  single, currency,  TDateTime ...

var forename, Surname, paysTax, totalHits,
  averageScore, initial;







var months: [];
  
var rainfall: [[]];
var ageRecords: [];

Also Date ...

Assigning variables

surname := 'Morrison';
averageScore := 24.56;
paysTax := True;
initial := 'N';
dateSubmitted := StrToDateTime(
                  '19/4/2014');
months[1] := 'January';
rainfall[2000, 1] := 15;

or  
rainfall[2000][1] := 15;

surname = "Morrison";
averageScore = 24.56;
paysTax = true; //lower case t
initial = 'N';  

dateSubmitted = new Date(2014, 4, 19);

months[0] = "January";
rainfall[0][1] = 15;

If statement
  One statement executed if true



  More than one statement executed if true


if (currentNum > max) then
  max := currentNum
else
  writeln('max still ', max);


if (currentNum > max) then
  begin
    max := currentNum;
    writeln('max increased');
  end
else
  write('max still ', max);


if (currentNum > max)
  max = currentNum
else
  console.log("max still " +
              max.toString());

if (currentNum > max) {
  max = currentNum;
  console.log("max increased");
}

else
  console.log("max still " +
                    max.toString());

Case/Switch statement

case iChoice of
  1 : DoThis;
  2 : DoThat;
end;

switch(iChoice) {
  case 1: DoThis();
          break;
  case 2: DoThat();
          break;
}

For loop

for i := 1 to 10 do
  begin
    lines of code
  end;
  
for i := 10 downto 1 do
  begin
    lines of code
  end;

for (var i = 1; i <= 10; i++) {
   lines of code
}


for (var i = 10; i >= 1; i--) {
   lines of code
}

While loop

while amount > 0 do
  begin
    lines of code
  end;

while (amount > 0) {
  lines of code
}

Repeat loop

repeat
  lines of code
until finished = true;

do {
  lines of code
}
while (!finished)

Defining records/structs

type
  TAgeRec = record
    surname : string;
    age : integer;
  end;

var ageRecord = {surname: "Cox", age: 17};

Arithmetical operators


+
-
*
/
DIV
MOD


+
-
*
/ (with a real operand)
/ (with integer operands)
%

Comparison operators

=


<>
<
>
<=
>=

==
Same value and type: ===

!=
<
>
<=
>=

Boolean operators

and
or
not

&&
||
!

Procedure without parameters

procedure ListRecords;
begin
  lines of code
end;


To call the procedure:
ListRecords;

function listRecords() {
  lines of code
};


To call the routine:
listRecords();

Procedure with parameter passed by value

procedure ListRec(recNo: Integer);
begin
  lines of code
end;


To call the procedure for the fifth record:
ListRec(5);

function listRec(recNo)
{
  lines of code
}
                  
                  
To call the function for the fifth record:
listRec(5);

Procedure with parameter passed by reference

procedure Calc(var total :
              integer);
begin
  lines of code
end;

var objTotal = {tot: 42};
function calc(total) {  
  total.tot += 1;  
}
calc(objTotal);
Function

function Cube(Num: double): double;
begin
   Cube := Num * Num * Num;
end;

function cube(num) {
   return num * num * num;
}

Inbuilt string functions
  Type conversions
                  
                  
  


  String manipulation


IntToStr converts integer to string
FloatToStr converts real to string
FloatToStrf converts real to formatted string
DateTimeToStr converts from TDateTime to string
StrToInt converts string to integer  
                  

Length returns length of  string



Pos looks for one string within another
and returns the position of the first matching character.

(StrUtils Unit)
MidStr returns the required number of characters
from any position in the string
LeftStr returns the required number of characters
from the front of the string
RightStr returns the required number of characters
from the back of the string
UpperCase  returns a string in upper case
LowerCase returns a string in lower case


toString() converts numeric value to string e.g.
max.toString()


var strNum = "5";
var intNum = Number(strNum);  

var alphabet =
     "abcdefghijklmnopqrstuvwxyz";
var stringLength = alphabet.length;
                  



var jklPos = alphabet.indexOf("jkl");
if (jklPos != -1)
  console.log("jkl position: " +
               jklPos.toString());                


alphabet.substr(9, 3)

alphabet.substr(0, 3)

alphabet.substr(-3)




alphabet.toUpperCase()
forename.toLowerCase()

 

 

Code of HTML5 File with JavaScript Functions to Test the Syntax in the Table

<!DOCTYPE html>
<script>
// Syntax test
function output() {
  /* Comment spanning  lines
     This function displays some output in the paragraphs with IDs
     and outputs also to the console.
  */ 

  var VAT_RATE = 0.175;
  var forename, Surname, paysTax, totalHits, averageScore, initial;
  var months = []; // one-dimensional array
  var rainfall = [[]]; // array of array
  var ageRecord = {surname: "Cox", age: 17};  // object
  var ageRecords = [];
  var currentNum = 2, max = 2;
  var objTotal = {tot : 42};
    
  function listRec(recNo) {   //nested function
    document.getElementById("p5").innerHTML = ageRecords[recNo].surname + ': ' + ageRecords[recNo].age;
  }; 
  function calc(total) {  //Changes the value of the contents of the object passed to it
    total.tot += 1;  
  }
  function cube(num) {
    return num * num * num;
  }	

  surname = "Morrison";
  averageScore = 24.56;
  paysTax = true; // lower case t
  initial = 'N';  // no char type so initial is a string of length 1

  dateSubmitted = new Date(2014, 4, 19);

  months[0] = "January";
  rainfall[0][0] = 15;
  document.getElementById("p1").innerHTML = "VAT_RATE: " + VAT_RATE; // Outputs 0.175
  document.getElementById("p2").innerHTML = "Rainfall for first month: " + rainfall[0][0]; // Outputs 15  
    
  if (currentNum > max) 
    max = currentNum
  else
    console.log("max still " +  max.toString()); // Outputs to console "max still 2"
    
  currentNum++;
  if (currentNum > max) { 
    max = currentNum;
    console.log("max increased"); // Outputs to console "max increased"
  }
  else
    console.log("max still " +  max.toString());
    
  var iChoice = 1; 
  switch(iChoice) {
  case 1: console.log("one");
          break;
  case 2: console.log("two");
          break;
  }
    
  ageRecords[0] = ageRecord;
  ageRecords[0].age += 1;
  document.getElementById("p4").innerHTML = "age: " + ageRecords[0].age; // Outputs 18
  
  listRec(0); // Outputs "Cox: 18"
  document.getElementById("p6").innerHTML = "objTotal.tot: " + objTotal.tot; // Outputs "objTotal.tot: 42"
  calc(objTotal);
  document.getElementById("p7").innerHTML = "objTotal.tot: " + objTotal.tot; // Outputs "objTotal.tot: 43"

  for (var i = 1; i < 4; i++) // Output 1 2 3
    document.getElementById("p8").innerHTML += i + " "; 

  var i = 1;
  while (i < 4) { // Output 1 2 3
    document.getElementById("p9").innerHTML += i + " "; 
    i++;
  }
  var j = 1;
  do {// Output 1 2 3
    document.getElementById("p10").innerHTML += j + " ";  
    j++;
  }

  while (j < 4); 
    document.getElementById("p11").innerHTML = cube(3);
  var iChoice = 1; 
  switch(iChoice) {
  case 1: console.log("one");
          break;
  case 2: console.log("two");
          break;
  }	
  var strMax = max.toString();
  var strNum = "5";
  var intNum = Number(strNum); 

  var alphabet = "abcdefghijklmnopqrstuvwxyz";
  var stringLength = alphabet.length;
                  
  var jklPos = alphabet.indexOf("jkl");

  if (jklPos != -1)
    console.log("jkl position: " +  jklPos.toString());  // Outputs to console "jkl position: 9"
  var jtol = alphabet.substr(9, 3);
  var atoc = alphabet.substr(0, 3);
  var xtoz = alphabet.substr(-3);

  console.log("jtol + atoc + xtoz: " +  jtol + atoc + xtoz); // Outputs to console "jtol + atoc + xtoz: jklabcxyz"               
  var alphUpper = alphabet.toUpperCase();
  var forename = 'JOE';

  var foreLower = forename.toLowerCase()
  console.log("alphUpper + foreLower: " +  alphUpper + foreLower); // Outputs to console "ABCDEFGHIJKLMNOPQRSTUVWXYZjoe"
}
</script>
<h1>JavaScript Syntax Test</h1>
<button type= "button" onclick = "output()">Display</button>
<p id = "p1"></p>
<p id = "p2"></p>
<p id = "p3"></p>
<p id = "p4"></p>
<p id = "p5"></p>
<p id = "p6"></p>
<p id = "p7"></p>
<p id = "p8"></p>
<p id = "p9"></p>
<p id = "p10"></p>
<p id = "p11"></p>

Programming - a skill for life!

How to apply your knowledge of Pascal when learning JavaScript