ArticleslgStudy

mathematics

JS++

JS++ is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand JS++ rather than just read about it. In short: JS++ is a programming language for web development that extends JavaScript with a sound type system. It includes imperative, object-oriented, functional, and generic programming features.

JS++ — main illustration
JS++ — illustration

Key takeaways

  • JS++ belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect JS++ to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of JS++ from memory before moving on to harder problems.

Reference excerpt

JS++ is a programming language for web development that extends JavaScript with a sound type system. It includes imperative, object-oriented, functional, and generic programming features. It is free and open-source software released under a BSD license.

History JS++ first appeared on October 8, 2011. The modern implementation was announced at DeveloperWeek 2016 and released on May 31, 2016. The language is designed by Roger Poon and Anton Rapetov.

Features

Sound gradual type system Since JS++ is a superset of JavaScript, declaring data types for variables is optional. However, when types are declared, the types are enforced at both compile time and runtime. Type annotations in JS++ use the traditional C/C++ syntax:

Notably, this differs from TypeScript and ActionScript, which use a more verbose style:

The type system in JS++ is sound for ECMAScript and DOM API corner cases, including host objects, dynamic key-value pairs, Comet, JScript conditional compilation, dynamic return types, ActiveX, ECMAScript for XML, web browser garbage collector and cyclic reference counting bugs, conditional logic, and other edge and corner cases. This differs from other JavaScript supersets where types are optional and discarded at runtime via type erasure, such as in TypeScript.

Importing JavaScript libraries JS++ can use JavaScript libraries using the one-line external statement as in the following example from the homepage of JS++:

Object-oriented programming While classes in JavaScript (ECMAScript 6) are syntactic sugar for prototypes under the hood, JS++ classes resemble the classes found in classical programming languages such as C++, Java, and C# in terms of memory layout, performance, and semantics. "Classes" are a static concept, and they cannot be altered at runtime (during program execution) as is the case for JavaScript, Smalltalk, Lisp, and TypeScript, which rely on prototypes. For example, private methods are private at both compile time and runtime, and external JavaScript objects cannot access private JS++ fields or methods—even if a reference to a JS++ object is obtained from JavaScript.

Example: object-oriented sorting The following source code illustrates object-oriented sorting in JS++ using the System.IComparable<T> interface and System.Comparison enumeration for type-safe and readable comparisons. The custom sorting logic is one line of code in the overridden compare() method below:

Thus, in the code above, the custom sorting logic provided is:

Likewise, to call the sort:

For printing the sorted results:

Example: encapsulation by default JS++ provides encapsulation by default. In the following example, the fields x and y are private by default, even if no access modifier is specified. The methods getX() and getY() are public by default. This enables a more concise class definition syntax, as illustrated in the Point class below:

Out-of-bounds analysis An out-of-bounds access usually occurs with arrays and other containers. For example, when we access the 100th element of a 3-element array, we have an out-of-bounds access:

In Java and C#, this can result in an exception and program termination. In C, this can result in buffer overflows or segmentation faults. C++ has varying semantics, such as default initialization, exceptions, segmentation faults, or buffer overflows. JS++ can efficiently analyze and prevent out-of-bounds errors at compile time. JavaScript has the notion of null and undefined values, where null means a value is present but it is an empty value, and undefined means there isn't a value there at all. JS++ extends this intuition further to differentiate between empty values and out-of-bounds accesses. Consider the following code, with a nullable int type represented with int?:

While nullable types can represent an out-of-bounds access, it falls apart when the array might contain nullable values as illustrated above. Instead, JS++ introduces an additional concept in addition to null values: undefined values. Recall that JS++ extends the JavaScript notion that null means a value is present but is an empty value, while an undefined value means a value does not exist at all. JS++ uses the concept of "a value does not exist at all" to mean an out-of-bounds access has occurred, and this concept is known in JS++ as "existent types." Therefore, the previous example can be amended. The existent type int+ means "int or out of bounds" and int?+ means "int, null, or out of bounds":

Intuitively, this means existent types cannot be used as the underlying type for array elements. JS++ enforces this at compile time:

Instead of following every conditional branch or virtual method call path, which would result in path explosion and exponential compile times, existent types have essentially the same compile-time analysis cost as int, bool, and other primitive types. Consequently, compile times have been shown to be unaffected (±1-2ms) by the introduction of existent types. Since existent types are used for all array and container types in JS++ (such as hash maps, System.Stack<T>, and System.Queue<T>), JS++ containers are thus guaranteed to not have out-of-bounds errors. In JS++, undefined is a value that cannot be changed. In JavaScript (ECMAScript 3), undefined is a mutable property of the global object, resulting in circumstances where "undefined" can be "defined." Thus, existent types would not have been possible in pure JavaScript, as arrays can contain elements with the undefined value, undefined can be defined, or other edge and corner cases that are prevented in JS++. Also, in comparison to Java and early object-oriented languages such as Eiffel, JS++ does not default initialize objects to null. Instead, the compiler enforces initialization by the programmer:

Therefore, since existent types are deeply embedded into the language, JS++ can guarantee that out-of-bounds errors never occur.

Databases The concept of existent types can be extended outside of containers. For example, in MySQL, columns can be nullable. If the row does not exist for a specified condition (e.g. WHERE clause), the undefined value can be returned. However, if the row does exist but the value at the column is empty, a null value can be returned instead. This can simplify the code and interfaces to the data access layer.

Integer types JS++ provides 8-bit, 16-bit, 32-bit, and 64-bit integer types as well as floating point types:

From the project homepage:

… excerpt ends here. Continue reading the full article.

Illustrations

JS++ illustration

Worked examples

Example 1 — a first encounter with JS++

Start with the simplest possible case. Write down what JS++ claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to JS++ before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about JS++ ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of JS++

In research
JS++ appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses JS++ in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
JS++ is common in secondary-school and first-year university syllabi. It links to neighbouring topics Class-based programming languages, Functional languages, High-level programming languages, so understanding it makes those chapters shorter.
In everyday life
Look for JS++ outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study JS++ in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what JS++ means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain JS++ out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is JS++ in simple terms?

JS++ is a programming language for web development that extends JavaScript with a sound type system. It includes imperative, object-oriented, functional, and generic programming features.

Why does JS++ matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study JS++?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on JS++.

Tags

  • Class-based programming languages
  • Functional languages
  • High-level programming languages
  • Programming languages
  • Programming languages created in 2011
  • Statically typed programming languages
  • Web programming

Keep exploring