This chapter presents the main features of JavaScript, at the level of basic constructs, types, syntax.
It will be especially useful if you have previously programmed in another language, well, or as a repetition of important points in the section.
Everything is very compact, with links to detailed descriptions.
Code structure
Operators are separated by a semicolon:
As a rule, a line break also implies a semicolon. So it will also work:
.. However, sometimes JavaScript does not insert a semicolon. For example:
There are cases when this leads to errors that are hard enough to find and fix.
Therefore, it is recommended to put semicolons. Now it is, in fact, the standard.
Single-line comments // ...
and multi-line /* ... */
are supported:
Read more: Code structure.
Variables and Types
- Declared a
var
directive. Can store any value: - There are 5 "primitive" types and objects:
There are also special numeric values Infinity
(infinity) and NaN
.
The value NaN
indicates an error and is the result of a numeric operation if it is incorrect.
We'll talk about objects in the chapter Objects as associative arrays; they are very different in JavaScript from most other languages.
- A
null
value is not a "link to a null address / object" or something similar. This is just a special value. It is assigned if we want to indicate that the value of the variable is unknown.
For example:
- The value
undefined
means “variable not assigned”. For example:
You can assign it explicitly: x = undefined
, but this is not recommended.
- In the name of the variable can be used any letters or numbers, but the number can not be the first. Dollar characters
$
and underscore _
are allowed along with letters.
Read more: Variables, Introduction to Data Types.
Interaction with the visitor
The simplest functions for interacting with the visitor in the browser:
- prompt (question [, default]])
- Ask a
вопрос
and return the entered string, or null
if the visitor clicked "Cancel". - confirm (question)
- Ask a
вопрос
and suggest buttons "Ok", "Cancel". Returns true/false
, respectively. - alert
- Display a message on the screen.
All these functions are modal , i.e. Do not allow the visitor to interact with the page until the response.
For example:
Details: User interaction: alert, prompt, confirm.
Features of operators
- To add lines, use the
+
operator. If at least one argument is a string, the other one is also cast to the string:
- The
===
comparison checks for exact equality, including the same type. This is the most obvious and reliable way to compare. The remaining comparisons == < <= > >=
perform a numeric type conversion:
There will be no casts when comparing two strings (see below).
Exception: null
and undefined
values behave in non-zero comparisons.
- They are equal to each other
null == undefined
and are not equal to anything else. In particular, non-zero. - In other comparisons with casting (not
===
), the null
value is converted to zero, and undefined
becomes NaN
(“error”).
This behavior can lead to non-obvious results, so it’s best to use ===
for comparison.
For example, a funny consequence of these rules for null
:
From the point of view of common sense is impossible. The value null
not equal to zero and no more, but null >= 0
returns true
! - String comparisons are lexicographical, characters are compared by their unicode codes.
Therefore, it turns out that lowercase letters are always larger than uppercase letters:
Read more: Basic operators, Comparison operators and Boolean values.
Logical operators
There are logical operators in JavaScript: AND (denoted by &&
), OR (denoted by ||
) and NOT (denoted by !
). They interpret any value as logical.
Do not confuse them with bitwise AND, OR, NOT operators, which also exist in JavaScript and work with numbers at the bit level.
As in most other languages, the logical operators use a “short cycle” of calculations. For example, the evaluation of the expression 1 && 0 && 2
will stop after the first AND &&
, since it is clear that the result will be false (zero is interpreted as false
).
The result of the logical operator is the last value in a short cycle of calculations.
It can be said in a different way: although the values are interpreted as logical, but the one that ultimately determines the result is returned without conversion.
For example:
Read more: Logical operators.
Cycles
- Three types of cycles are supported:
12 | for ( var i = 0; i < 10; i++) { |
- A variable can be declared directly in a loop, but it will also be visible outside it.
-
break/continue
directives to exit the loop / go to the next iteration are supported. To exit simultaneously from several levels of the cycle, you can set a label.
Syntax: " имя_метки:
", it is put only before cycles and blocks, for example:
The transition to the label is possible only from the inside of the cycle, and only to the external block in relation to this cycle. In an arbitrary place of the program can not go.
Read more: Break and continue directives.
switch
design
For comparisons in the switch
construction, the ===
operator is used.
For example:
Details: switch design.
Functions
Syntax of functions in JavaScript:
-
sum
is the name of the function, the restrictions on the name of the function are the same as on the variable name. - Variables declared via
var
inside a function are visible everywhere inside this function, if
, for
, etc. blocks. visibility is not affected. - Parameters are passed "by value", i.e. copied into local variables
a
, b
, with the exception of objects that are passed "by reference", we will discuss them in detail in the chapter Objects as associative arrays. - A function without
return
is considered to return undefined
. Calling return
with no value also returns undefined
:
Read more: Functions.
Methods and properties
All values in JavaScript, with the exception of null
and undefined
, contain a set of auxiliary functions and values accessible "through the dot".
Such functions are called "methods", and values - "properties".
For example:
Strings also have a toUpperCase()
method that returns an uppercase string:
Read more: Methods and properties.
Total
In this chapter, we repeated the main features of JavaScript, the knowledge of which is necessary to bypass most of the “rakes”, and just to write good code.
Comments
To leave a comment
Scripting client side JavaScript, jqvery, BackBone
Terms: Scripting client side JavaScript, jqvery, BackBone