Comparison Table for Pascal and JavaScript
Language Feature | Pascal | JavaScript |
---|---|---|
Statement separator | ; | ; (or new line) |
Comment | //Comment after code |
//Comment after code |
Quoted string | 'Hello World' |
"Hello World" or 'Hello World' |
Code block | begin |
{ |
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 |
var forename, Surname, paysTax, totalHits, |
Assigning variables |
surname := 'Morrison'; |
surname = "Morrison"; dateSubmitted = new Date(2014, 4, 19); |
If statement |
|
|
Case/Switch statement |
case iChoice of |
switch(iChoice) { |
For loop |
for i := 1 to 10 do |
for (var i = 1; i <= 10; i++) { |
While loop |
while amount > 0 do |
while (amount > 0) { |
Repeat loop |
repeat |
do { |
Defining records/structs |
type |
var ageRecord = {surname: "Cox", age: 17}; |
Arithmetical operators |
|
|
Comparison operators | = |
== |
Boolean operators | and |
&& |
Procedure without parameters |
procedure ListRecords; |
function listRecords() { |
Procedure with parameter passed by value |
procedure ListRec(recNo: Integer); |
function listRec(recNo) |
Procedure with parameter passed by reference |
procedure Calc(var total : |
var objTotal = {tot: 42}; function calc(total) { total.tot += 1; } calc(objTotal); |
Function |
function Cube(Num: double): double; |
function cube(num) { |
Inbuilt string functions |
|
|
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>