JavaScript after Pascal

Introduction

This is the start of a section to help you to apply your knowledge of Pascal when learning JavaScript. (It is easier to make use of JavaScript via Smart Pascal for most tasks, but Smart Pascal gives you the opportunity to write your own JavaScript code within asm blocks if the existing functionality does not meet your needs). We now provide a sample lightweight Smart Mobile Studio application to demonstrate how you can readily inspect the JavaScript output from your Smart Pascal source code.

We recommend the W3Schools JavaScript tutorial as a great resource; it provides you with the ability to modify the code of sample programs and to test the effect of your changes online.

When you try a new language you will be looking out for features such as:
  • case sensitive (JavaScript) or case insensitive (Pascal);
  • strongly typed (Pascal, where once an integer, always an integer) or weakly typed (JavaScript);
  • rules for naming identifiers: in JavaScript identifiers can contain letters, digits, _, and $ but must not begin with a digit;
  • inbuilt primitive types such as integer and string;
  • arrays, lists, and records/structs;
  • operators for purposes such as assignment, addition, subtraction, multiplication, division (including integer division), comparison and Boolean expressions;
  • selection by if and case/switch;
  • loops;
  • procedures and functions (and their parameters passed by value or by reference: References are passed by value. If you pass a reference to an object, you are passing a copy of that reference but you can then change the contents of the referenced object. See Alnitak's answer on stackoverflow.);
  • inbuilt routines e.g. for string manipulation;
  • other means of splitting up and reusing the code such as by classes and files: see local storage;
  • inbuilt classes including visual components such as forms, edit boxes, labels and combo boxes: JavaScript can manipulate HTML5 widgets and frameworks such as JQuery have more impressive widget sets;
  • enumerated types (improvised in JavaScript) and sets;
  • saving and loading data and database access;
  • error trapping: JavaScript uses try ... catch ... finally instead of try ... except ... finally;
  • event handling: the output() function in our test script on the following page is assigned to the onclick event of the button;
  • pointers/references: there are no pointers in JavaScript and we show how to pass references to objects;
  • networking capabilities.

It is convenient to code JavaScript in Notepad++, which syntax highlights the code. The menu item Run > Launch in Chrome (with a key combination shortcut such as Ctrl+Alt+Shift+R for Chrome) makes it convenient for testing.

The next page shows a comparison table giving the JavaScript equivalents of fragments of Pascal code. The table is followed by the full code of the JavaScript functions that we used to test the syntax of the fragments in the table.

Follow the numbered links to pages on the HTML5 canvas and widgets, jQuery widgets, local storage and sets.

Programming - a skill for life!

Developing graphical web pages including 3D, input from keyboard and mouse and storing and loading data