As noted elsewhere, JavaScript was created by Netscape, and given its
unfortunate name for marketing reasons.
Standard Javascript is more properly called ECMAScript, for the
ECMA which publishes the standard,
ECMA-262.
ECMA stands for the
European Computer Manufacturers Association,
but it now just calls itself ECMA International. It publishes
several standards for the industry.
The language mostly answers to JavaScript.
Javascript was created to run in the browser, but it is first
a programming language, and can be run in other contexts, including
on the server side.
A couple of ways to just “try out” Javascript expressions.
Use the console in the browser.
Chrome:
⋮ / more tools / developer tools / console
On Fox:
tools / browser tools / web developer tools / console
You will find a list of error messages and a prompt at which you can type
Javascipt code.
Each run the Javascript language, but in different contexts, and with
different builtin objects.
Same comments as Java.
Semicolons are terminators, as in C, Java and friends, but are implied
by line breaks and some punctuation, so they may often be omitted. Better
not to.
Displaying values.
The console will print values of expressions.
Values can be printed with console.log(...).
Variables
Declared with var, and constants with const.
Case sensitive.
Dynamically typed.
C-like scoping rules: Variables may be global, or belong to a scope
enclosed in { } .
Variable names.
Start with letter or _ and contain letter, digit or _ as usual.
Can also start with or contain $.
Basic types
Boolean.
undefined (value of uninitialized variables).
null (different from undefined)
Number (integer or float, represented as a large float).
BigInt (unbounded integer)
String
Symbol (unique identifier)
Object (which includes functions and several important built-in types).
Usual numeric operators, +, -, *, / and %, and update forms.
Also ++ and --.
Conversions
Since types are dynamic, no conversion on assignment.
Using + to combine a string and number converts the number to string.
Other numerical operators convert the string to a number, and
give NaN if impossible.
Undefined, null, 0, NaN and "" convert to false. Everything else
converts to true. (Including "0" and empty arrays.)
Comparison operators.
Usual: ==, !=, <, >, <=, etc.
Comparisons will try to convert args to the same type before
comparison, so "42" == 42 is true.
=== for identical: equal and of the same type. Suppresses
conversions, so "42" === 42 is false.
Also !==
Exceptions
throw. You may throw any type.
The system and libraries throw Error or its descendents.
The try/catch has the familiar syntax, without a type in the catch.
Finally. Follows catch, and is run last. Does not take an argument.
try {
// Attempt something
} catch(debris) { // No type declaration! Not java
// Handle error
}
finally {
// Any cleanup that applies to either outcome.
}
Control flow.
Curly braces for blocks, as Java.
if else
while, do ... while
for()
Statements may be labeled foo: m = 10
break, continue. These may take a label, so you can break (or
continue) a loop other than the nearest one.
switch, using break, like Java. Unlimited value type.
for...in and for...of, which we'll discuss later.
Strings
As in Java, Strings are a type of object.
Constants with 'single quotes' or "double quotes". End with the same start.
Non-number and negative subscripts are treated as hash keys. (Kind
of nuts. PHP is nuts in a similar way.)
Functions
Normal function definition
function name(arg1, arg2, ...) {
statements
}
Anonymous lambda function assigned to a variable
var f = function(arg1, arg2, ...) {
statements
}
The second is a lambda, which are becoming more common in many languages. This lambda
closure is then assigned to a variable, but they are often passed as parameters.
Arguments passed in the same way Java does, by value for simple
things and by reference value for objects.
Functions may be called with fewer arguments than parameters, and
the extra arguments get undefined.
Functions always have an arguments object, which is an
array of all arguments sent.
Functions may be called with more arguments than parameters. This
can be used to access extras.
Default values are allowed. Default is used instead of undefined
if the parameter is not sent.
function(a,b, ...rest)
Assigns the third and following parameter to the array-like rest.
var f = (arg1, arg2, ...) => { statements }
Also creates an anonymous function.
Can omit the brackets for one statements.
Lighter implementation for efficiency. Leaves out some
capabilities that you probably weren't using anyway.
Object constants.
Object constants are key/value lists, much like hashes in other languages.
var x = { frank: 10, bill: 30, sally: "string" };
x.bill
Some fields can be functions, which are methods of the objects.
Method functions refer to their own fields with this
Far more to say about objects.
Objects can be created by functions.
function joe(i) {
this.size = i;
this.log = function() { console.log("My size is " + this.size); }
}
var j = new joe(12);
j.log();
Context.
Every Javascript program runs in a context. In the browser,
each window has a context.
The context is represented by an implicit object, which you can refer
to as this when not obviously running in some other object.
Contents can be referred to as this.name or just name.
Contents
Self-references, alias for root, such as window.
References to roots of related windows, such a parent or child frames.
Built-in functions, including for controlling the window.
Built-in basic classes, such as String and Math.
location object. Current URL and some other things.
history object. Limited use in current JS for security reasons.
screen object. Display parameters.
document object. The document tree, or DOM tree. Where
the action is.
document.head
document.body
.children[i]
Some browsers add all elements having a name or id attribute, so
you can refer to DOM objects directly by their id.