Javascript
Overview
There is no any preparation or compilation of JS.
JS can be executed not necessarily in browser but anywhere where there is a JS engine:
V8 (Chrome, Opera, Edge)
SpiderMonkey (Firefox)
Engine parses the code -> converts to machine code -> the code is executed
"use strict"
or 'use strict
' always in the beginning of the file (So, for now "use strict"; is a welcome guest at the top of your scripts. Later, when your code is all in classes and modules, you may omit it.)
Syntax
Variables
Variables named
apple
andAPPLE
are two different variables.The name must contain only letters, digits, or the symbols
$
and_
.The first character must not be a digit.
Data types
dynamically typed language
The number type represents both integer and floating point numbers.
There are many operations for numbers, e.g. multiplication *
, division /
, addition +
, subtraction -
, and so on.
Besides regular numbers, there are so-called “special numeric values” which also belong to this data type: Infinity
, -Infinity
and NaN
.
In JavaScript, the “number” type cannot safely represent integer values larger than (253-1)
(that’s 9007199254740991
), or less than -(253-1)
for negatives.
The boolean type has only two values: true
and false
.
The special null
value does not belong to any of the types described above.
It forms a separate type of its own which contains only the null
value:
In JavaScript, null
is not a “reference to a non-existing object” or a “null pointer” like in some other languages.
It’s just a special value which represents “nothing”, “empty” or “value unknown”.
The special value undefined
also stands apart. It makes a type of its own, just like null
.
The meaning of undefined
is “value is not assigned”.
If a variable is declared, but not assigned, then its value is undefined
:
Technically, it is possible to explicitly assign undefined
to a variable:
…But we don’t recommend doing that. Normally, one uses null
to assign an “empty” or “unknown” value to a variable, while undefined
is reserved as a default initial value for unassigned things.
Symbol
Object
Type conversion
String Conversion
– Occurs when we output something. Can be performed with String(value)
. The conversion to string is usually obvious for primitive values.
Numeric Conversion
– Occurs in math operations. Can be performed with Number(value)
.
The conversion follows the rules:
Value | Becomes… |
---|---|
|
|
|
|
|
|
| The string is read “as is”, whitespaces (includes spaces, tabs , newlines etc.) from both sides are ignored. An empty string becomes |
Boolean Conversion
– Occurs in logical operations. Can be performed with Boolean(value)
.
Follows the rules:
Value | Becomes… |
---|---|
|
|
any other value |
|
Most of these rules are easy to understand and memorize. The notable exceptions where people usually make mistakes are:
undefined
isNaN
as a number, not0
."0"
and space-only strings like" "
are true as a boolean.
Comparison
Treat any comparison with
undefined/null
except the strict equality===
with exceptional care.Don’t use comparisons
>= > < <=
with a variable which may benull/undefined
, unless you’re really sure of what you’re doing. If a variable can have these values, check for them separately.
Last updated