Statement
The execution unit of a JavaScript program is line, which is a line of execution. In general, each line is a statement.
A statement (statement) is an action that is performed to accomplish a task, such as a line assignment statement.
var a = 1 + 3;
This statement first var
declares the variable with a command, a
and then 1 + 3
assigns the result of the operation to the variable a
.
1 + 3
Called expression, a formula for getting the return value. The difference between a statement and an expression is that the former is primarily intended to perform some sort of operation, and generally does not require a return value, while the latter is a value that will be returned for the return value. Expressions can be used wherever expected values are in the JavaScript language. For example, the assignment statement to the right of the equals sign is expected to be a value, so you can place various expressions.
Variable
A variable is a named reference to a value. A variable is called a "value", and then the name is referenced, which is equivalent to referencing the value. The name of the variable is the variable name.
Simply put, a variable is a container for storing data.
In JS the variable is created by the keyword "var", and the process of creating the variable is called "declaration"
var B = 3;
The above code is the first declaration of the variable B, and then the value 3 is assigned to the variable B, the reference to the variable B will be given a value of 3.
Variable Promotion
The JavaScript engine works by parsing the code, getting all the declared variables, and then running on a line. The result is that all declarations of variables will be promoted to the head of the code, which is called variable elevation (hoisting).
For example, this code:
var a = 4;
We can understand this as:
var a;a = 4;
Identifier
In JS all can be named by us can be called Identifiers (identifier), the most common identifier is the variable name and function name.
Identifiers have their own naming conventions, which are named after the name, and the identifiers that do not conform to the rules are illegal identifiers.
1) The first character, which can be any Unicode letter (including letters in English letters and other languages), as well as the dollar sign ( $
) and underscore ( _
).
2) The second character and subsequent characters, in addition to Unicode letters, dollar signs and underscores, can also be used as numbers 0-9
.
3) identifiers cannot be keywords or reserved words in ES
1a //cannot be a number as the first character @qbb //cannot be in addition to the dollar sign $ and underline _ symbol other than the first character ab^c //In the middle cannot contain symbols other than dollar sign $ and underscore _
All of the above are illegal identifiers
! Chinese can be used as an identifier because Unicode encoding is compatible
var I'm a variable = 1
Comments
Annotations are not executed in JS, and the function of annotations is to interpret the code. There are two ways to annotate JS:
Single-line Comment/* This is a multiline comment */
Data type
Each value of a JavaScript language belongs to a data type. There are six types of JavaScript data. (ES6 added the value of the seventh Symbol type)
- Value (number): Integers and decimals (for example
1
, and 3.14
)
- String: Text (for example
Hello World
).
- Boolean value (Boolean): two special values that represent authenticity, i.e.
true
(true) and false
(false)
undefined
: means "undefined" or nonexistent, that is, because there is no current definition, so there is no value for the time being
null
: Represents a null value, that is, the value here is null.
- Object: A collection of various values.
Typically, the three types of numeric, string, and Boolean values, collectively, are the values of the original type (primitive type), i.e. they are the most basic data types and can no longer be subdivided. An object is called the value of a synthetic type (complex type), or as a reference type, because an object is often a composition of values of more than one primitive type, and can be thought of as a container that holds various values. As undefined
null
for and, they are generally regarded as two special values.
Objects are the most complex data types and can be divided into three subtypes.
- Narrow Objects (object) (key-value pair type)
- Arrays (Array)
- Functions (function)
typeof operator
We can use the TypeOf operator to determine what type of data a value returns
Values, strings, and Boolean values are returned number
, respectively string
boolean
.
typeof 123 //"number" typeof ' Hello ' //"string" typeof true //"Boolean"
Undefined return undefined
typeof undefined //"undefined"
NULL returns an Object
typeof null//"Object"
Objects and Arrays return object
typeof [] //"Object" typeof {}//"Object"
Functions return function
function f () {}typeof f () {} //"function"
JS Basic concepts and data types