JavaScript/Relation to other languages

From Wikibooks, open books for an open world
Jump to navigation Jump to search

If you have programming experience or training with another language, learning JS will be easier and more difficult at the same time. Especially the fact that JS is a member of the C-family languages (C, C++, Java, ...) may help and confuse simultaneously. It uses the common syntax but implements concepts that differ from the other family members. Therefore it might entice you with this C- or Java-like syntax. Underneath, it is an entirely different beast. The semantic and design are influenced by the programming languages Self and Scheme.

The easy part for C or Java programmers will be picking up the syntax. Most parts of the flow control, logic, and arithmetic are the same. After diving deeper into the language, you will notice the differences. Here are some distinctions to Java.

Behavior of Variables[edit | edit source]

Starting with the obvious, JS is a loosely typed language. There are several implications for this:

  • Integers and floats are both represented by 64-bit floating point numbers (but crazily enough, bitwise operations are still available and sometimes faster).
  • You may change a variable's type at will.
  • Objects consist of key/value pairs, labeled 'properties'. Values can be changed, and complete properties can be added or removed from an object at will.

The list goes on, and we are granted extraordinary powers to do wonderful and sometimes incredibly stupid things whilst programming. So it's very important to keep sober-minded when attempting to harness the power of the dynamic variables.

Scope of Variables[edit | edit source]

  • From module to module: In Java the key-words public, private, and protected defines the visibility of variables to other modules (classes). In JS there is only the simple export/import resp. module.exports/require mechanism.
  • Within a module: In JS the visibility within a module is defined by the key-words var(outdated, don't use it anymore), let, and const. Java knows the keyword var too, but its semantic is a little different. The JS const is equivalent to Java's final.
  • Within blocks: 'Blocks' are code sequences surrounded with brackets { }. The visibility of variables concerning blocks is identical in JS (using let or const) and Java: The variables are visible within the defining block and the included blocks but not outside of the defining block.
  • Closure: A closure is an - anonymous or named - function that can refer to variables of its enclosing context. In this case, the function 'sees' the variable's value as of the moment the function was created but not as of the later moment when it is called. JS has used this concept since its early days. Java uses similar but not identical technics to access variables of outer context: inner classes and lambda expressions.

Classes[edit | edit source]

Unlike Java, JS is a classless language. Instead, JS uses prototypes to implement object-oriented features like inheritance. Classes can be simulated, but it is only 'syntactically sugar', and if you remove the idea of classes from your head, the learning process will become easier.