Jump to content

JavaScript/Introduction

From Wikibooks, open books for an open world




JS is a programming language that implements the international standard ECMAScript. It is based on the following concepts.

Dynamic data types

JS knows some primitive data types (Number, String, Boolean, BigInt, Symbol, Undefined, Null) and diverse derivates of the data type object (Array, Date, Error, Function, RegExp). [1] [2] If a variable exists, its type is clearly defined. But the type can be changed at any time by assigning a value of a different type to the variable, e.g.: the code fragment let x; x = 'Some text'; x = 2; x = [10, 11, 12]; is perfectly correct. It will not create a compile-time or run-time error. Only the type of the variable x changes from Undefined to String to Number and lastly to Object/Array.

(Note: JSON is a text-based data format, not a data type. As such, it’s language-independent. It uses the JavaSript object syntax.)

Functional programming

Functions are first-class citizens similar to variables. They can be assigned to variables, passed as arguments to other functions, or returned from functions. The code fragment function sayHello() {return 'Good morning'};let x = sayHello; console.log(x()); creates a function sayHello, assigns it to the variable x, and executes it by calling x().

Object-orientated programming

JS supports object-oriented programming and inheritance through prototypes. A prototype is an object which can be cloned and extended. Doing so, a prototype chain arises. This differs from other OO-languages, e.g. Java, which uses classes for object-oriented features like inheritance. Nevertheless, at the syntactical level, classes are available in JS. But this is only 'syntactical sugar'. Under the hood, JS uses the prototype mechanism.

C-like syntax

The JS syntax is very similar to that of C, Java, or other members of the C-family. But we must always consider that the concepts and runtime behavior are distinctly different.

Relation to Java

JS has no relation to Java aside from having a C-like syntax. To avoid possible confusion, we would like to highlight some distinctions between JS and Java clearly.

In the beginning, Netscape developed JavaScript, and Sun Microsystems developed Java. Java includes classes and object instances, whereas JavaScript uses prototypes. In Java, variables must be declared before usage, which is unnecessary (but not recommended) in JS.

In Java, variables have an immutable static type (int or String, for example) that remains the same during the complete lifespan of a running program. In JS they also have a type (Number or String, for example), but this type can change during the lifespan of a running program. The type is detected from the environment. Therefore it's not necessary and not possible to define the type explicitly.

int x = 0;          // Java: 'name of type', 'name of variable', ...
let x = 0;          // JS: 'let' or 'const', 'name of variable', ... 
                    //     The type will be 'Number' because of the right side of the equal sign.
let x = String (0); // JS: explicit change from 'Number' to 'String' BEFORE assignment to x
                    //     The type will be 'String'. Test it with: alert(typeof x)

JS engines

JS can run on the client-side as well as on the server-side. First versions of JS have run in Browsers that acted as mere interpreters. Today, the language is handled by just-in-time compilers (JIT). They parse the script, create an Abstract Syntax Tree (AST), optimize the tree, generate a JIT-specific bytecode out of the AST, generate hardware-specific machine code out of the bytecode, and bring the machine code to execution. Such just-in-time compilers exist not only in Browsers. They can also be part of other applications, e.g.: node.js which is written mainly in C++.

Widely used JS engines are:

  • V8 from Google: Google Chrome, Electron, Chromium, node.js
  • SpiderMonkey from Mozilla, Firefox
  • JavaScriptCore from Apple, Safari
  • ActionScript from Adobe, Flash

References