JS Basic syntax
- is case-sensitive in JS.
- Identifiers: Refers to variables, arrays, functions and other names. Defining the identity is actually the process of naming the word.
Hard requirements: means must be satisfied, otherwise it cannot be compiled
Not satisfied
Hard-to-ask content:
1. Consists of numbers, letters and underscores, and $, but cannot start with a number, prohibit the use of ES inside the keywords and reserved words (in fact, es in the built-in identifier).
2. Case-sensitive
Soft requirements content: Wang Wen Zhi yi
Naming laws
Hump Naming method: Hump Name method divided into two, uppercase is the big hump (Pascal named), the first letter lowercase is a small hump.
Hungarian nomenclature: It consists of one or more words. A fixed prefix is added to the front. This fixed prefix indicates the type of the variable.
A_li
Snake nomenclature: Refers to the words, the following lines are divided
User_name
This nomenclature, commonly common Linux kernel, C + + repository, is common in Ruby.
About the comments inside ES
Two kinds:
Single line://
Multiple lines:/**/
Strict mode
The introduction of a model from ES5, more rigorous, reduce ambiguity, resolution faster, can be used as the next version of the reference.
How do I get into strict mode?
"Use strict" defined used/strict/undefined
JS statement
- As with most programming languages, each statement ends with a semicolon and can be left out, but given the late compression, it is advisable to develop the habit of writing semicolons.
- Keywords and reserved words
Keyword: refers to the identifier defined in ES, we can no longer use these keywords when we name ourselves: else if break
Reserved word: The so-called reserved word refers to the word that may be changed to a keyword after the version is updated
Variable
Is that the data stored in it can change at any time.
Declare before using a variable, 3 keywords: var let const
Var is used by ES6 before it is declared.
Let and const are the new ES6.
Syntax for declaring variables:
Keyword variable name
Var I
(not assigned, default is undefined)
The Declaration and replication of variables can be done at the same time, if the variable is assigned a value for the first time, then this procedure is called the initialization of the variable.
Var i=5;
Multiple variables can be declared at once
When declaring a variable, you can only perform assignment operations, not other operations, such as + =.
- var supports repeating declarations such as: I=5 i=10
- Declare, then define unassigned, unassigned is ignored
- Support omission no declaration, direct assignment
Since JS is an explanatory language (Dynamic language), a declared variable can store any type.
Let, the above 1.2.3 declaration attributes are not supported
Characteristics:
1. Variable promotion (important)
It is not recommended to use VAR to declare variables because VAR declares that there is a variable promotion. The so-called variable promotion means that the declaration of a variable is promoted to the top of the current scope.
Console.log (i);
Var i=10;
Var i;
Console.log (i);
i=10;
Let declaration of the variable does not exist variable promotion, will be directly error.
A second difference:
var declares a variable with no block-level scope, and in other languages (Static), a block-level scope can be defined by a pair of {}.
Let declares that there is a block-level scope
Constant: Refers to an invariable amount
Const: is also a way of declaring a variable, but the declared variable cannot be changed again.
JS Data Type Hair Classification (* *)
Two main categories:
Basic data type (original value, simple value)
Reference data type (complex value)
Simple value: Refers to the simplest data segment, can not be split again, common simple values: let i=5
Complex values: Complex values are data that is composed of multiple simple values that can be split.
Common complex values: let arr=[1, "hello"]
Arr is an array name and is an address
Simple values are stored in the stack area, and complex values are stored in the heap.
Strip: i=5 arr ellipse: [...]
Because the size space occupied by the original data (simple values) is fixed, it is stored in the stack area. The reference type data, the size is not fixed, so the stack can not open up a suitable space. So the reference type of data is stored in the heap area.
The difference when copying data:
If it is a simple value, then we copy the copy of the value. If it is a reference type, the address is copied.
For a comparison of simple values and complex values:
If it is a simple value, then the comparison is the value, that is, the value is equal to two variables equal. If it is a reference type, then the address is compared.
Data type
In ES5, there are 5 basic data types: Undefined, NULL, number, Boolean, String ES6 Start, change to 6, add or subtract symbol
Undefined: Only one value, Undefined
When no value has been declared, (typeof i)
No statement, direct error
Null data type
Only one: null, represents null, undefined can be said to be null derived.
null==undefined true
Boolran Data types
True false
Strictly case-sensitive
Any data type can be converted to a Boolean type
Number any numbers that are not 0 will be converted to true 0 to False
String any non-empty strings will be converted to true, including spaces in quotation marks or 0 empty strings to False
In a Boolean function, you can convert other data types to bool values
The following 9 values are converted to a Boolean type of false
"'" ' string templates 0 and -0 NaN false null undefined
Number data type
Numbers generally we can be divided into integers and real numbers
Integer: can also be divided into positive and negative integers
With respect to integers, there are different binaries. Different binary, the front need to add different special symbols
Binary: 0b
Octal: 0, 0o
Hex: 0x
When used for calculations, it is converted to 10 binary
Real number: It's actually a decimal.
2 Methods: Decimal type (3.14), exponential type: 3.14e2 (10 of 2), 3.14e-2 (10 minus 2 times)
Maximum and minimum values:
Min-value Max-value can view the maximum and minimum values supported in ES
Console.log (Number.max-value)
There are infinite concepts, which are divided into positive infinity and negative infinity.
Positive: Infinity (Math.pow (2,1024))
Negative:-infinity (-math.pow (2,1024))
NaN is all called not a number meaning this is not a count, this thing is an identity: representing this variable is a non-number.
- 1. Classification as number type when printing
- 2. Design to any type of calculation, will return Nan
- 3.NaN is not equal to any value, including its own
There is a function in ES that is specifically used to determine whether it is a non-number: IsNaN
() is the number is false, is not true
numeric conversion correlation function: Number () parseint () parsefloat ()
Number (): Convert a variable to a numeric value
If it is a Boolean value, convert to 0 and 1
If it is null, it is converted to 0
If the undefined is converted to Nan
Var i;
i+=2;
Console.log (i);//nan
About strings using number () Translation rules:
If it's a decimal number, it's decimal.
If it is octal, it will discard the previous 0;//with 0o
If it is hexadecimal and binary, then it will normally be converted to decimal.
If it's not a pure number with a letter inside it, it will be Nan.
If it is an empty string, convert to 0
parseint (): Also converts a variable to an integer. and number () have one of the biggest differences, which are converted to numbers when converted.
Must start with a number ("123ABC")
parseint (NULL)//nan
parseint (undefined)//nan
parseint () receives the second number, indicating how much of the previous binary
(parseint ("12,8")) denotes 8 binary
Parsefloat (): Converted to decimals, and parseint () as much as possible to keep the book 3.14.15.67 3.14
Before ES6, number (), parseint (), parsefloat () are methods based on global objects, but starting with ES6, the above method and the number object are categorized.
String data type
Represents the string, in JS, single double quotes, no difference
If the string is out of double, inside the order, and vice versa, be sure to add a slash, you can use the escape character, and the backslash
Functions that can convert other data types to string types: ToString () string ()
ToString () can be converted to a string type except for null and undefined
String () can summon any type
- 1. (i.ToString (), typeof i.ToString ())
- 2. (ToString (null), typeof String (null))
ToString () can pass a parameter that indicates a binary
The string type is very overbearing, and the other data types are added to get the string.
ES6 added a new string template
Using two anti-quote ' '
First feature: Support for multiple lines of text
Second: You can parse the variable, you need to put the variable name in ${}
typeof: Returns the data type of a variable
Nan returns number
Infinity return number
Null to return object objects
Undefined return undefined
Type conversion: divided into two kinds
Implicit conversion: Refers to the automatic conversion within the system, usually occurs at different data type operations.
2+undefined //number
2+null //2
"2" +nan; //2 NaN
Show conversions: That's what we programmers are forcing data type conversions
Turn Boolean: Boolean ()
RPM: Number () parseint () parsefloat ()
Go to String: ToString () string ()
Quick way to convert data:
Turn Boolean: Just add two to the front of the variable!! , which can be converted to a Boolean value that the current type conforms to.
!! True
!! False
Transfer number: direct or/1
Null*1 //0,number
UNDEFINED/1 //nan,number
Go to string: Add to empty string
Operator
Unary operators: Acting on one operand
What you need to do frequently is + + and--
++a first self-increment and re-operation
a++ first operation and then self-increment
PS: Minimize + + 、--put one line, reduce the difference
Logical operators: With or not
Non -.! Take counter
And. && true True or false in Es not only return the true and false, if the inside said an operand is true, will return the second operand
Short-circuit phenomenon: When the first operand is false, then the second number is not judged, because the result is false.
Special phenomenon: Console.log (Null&&nan)//null
5&&nan//nan
5&&null//null
or | | Or inside there is a true, it returns true
There is also a short circuit phenomenon
Arithmetic operators
Arithmetic: addition, subtraction, multiplication, division, remainder, exponential operation (ES6 new)
Divide the certificate in Ps:js to get decimals
Index: (Math.pow (2,3));
(2**3);
Relational operators
Greater than, less than, greater than or equal to, less than equals, equal, unequal, congruent, not congruent
The letter can also compare size, compare the ASCLL code corresponding to the character encoding
a:97 b:98
If the characters and numbers are compared, there are two cases
First, the implicit conversion to a number is compared, if not, then turn to False
Equal and congruent
= = As long as the values are the same, regardless of data type
= = = Data type and value are the same
Special cases:
null==undefined//true
null===undefined//false
Nan===nan//false
Assignment operators
In addition to the = thought, there is also a set of compound assignment operators: + = = /= *=
Trinocular operator
Syntax: An expression? Expression 2: Expression 3;
Comma operator
Typically used for one statement execution, multiple operator operations
Let i= (1,2,3,4,5)
I=5
Let I=1, a=2;
Operators are prioritized and can be promoted with ().
Basic syntax, data types, and type conversions in JavaScript