JS writing page Effects dynamic script type language
Variables: Storing data (things in everyday life, such as TV, cell phone, computer, year of birth ...) )
Grammar:
var Obj=value;
Obj cannot start with a number and is case-sensitive
Value corresponds to the data type, such as number string bool null undefined
Concept
Store a data storage space in memory and an alias
Use
Create
Add an alias by using the Var method
var obj;
Assign value
Saves the data to the right of the equal sign to the left of the equals sign
Obj=value;
Take value
Use the variable name act up anywhere using the value in the variable
Console.log (obj);
Shorthand: Var obj=value;
****
Declaration in advance
Before executing the program, the JS engine will read all variables declared by Var
Focus to the top and the assignment will stay in place
The original
Console.log (i);//undefined
var i=0;
Console.log (i);//0
After the engine has been modified
var i;
Console.log (i);//undefined
i=0;
Console.log (i);//0
Only the unassigned value is declared and a undefined appears
var obj;//undefined
Assignment only is not declared (strongly not recommended, although no error is available)
m=12;
Unassigned not declared obj is not defined
K
Prototype type (base type)
Number: Value
var s1=1,s3=2.5;
Special Case: Rounding error
May be in the calculation process, may appear in the calculation of the situation, through
Rounding way to handle: i.tofixed (int)
Occupies 8 bytes
1kb=1024b
1GB=1024MB;
String: Strings
It can be done by quotation marks (double, single).
Use of international standards: Unicode
Once created cannot be modified, you can only create or replace
One English, the number is 1b, a Chinese character 2b
When to use a number (because the number is fast)
such as price, quantity, age, and so on, as long as not including non-digital use number
Use string as long as it contains a non-numeric
Boolean: Boolean
True--1
false--0
Undefined: Not defined
Null: Empty
Other:
Strong typing: Developing large-scale bottom-level applications: C, C + + Java
Weak type: Developing High-level applications (web): Js,python, PHP
Characteristics
You do not need to specify a data type when declaring a variable
The same variable, you can use different data types successively
It will convert the data type ' automatically ' according to its own situation.
Automatic (implicit) conversion
Do not need programmer intervention, JS automatically help our programmers to complete the
In arithmetic operations
+,-,*/ %
By default, data is converted to a number and then calculated
var s1=1,s2= ' 2 ';
S2-s1 s2*s1 s2/s1 ....
+ (emphasis)
If 2 variables are calculated, one of the two
is a string, then string concatenation is used
If the conversion is not numeric, NaN (not a number)
Manual (Force) conversion
Programmers perform types of operations by invoking certain functions
2 methods for any type of transfer to a string
X.tostring ():(can be processed and object-oriented except for null and undefined)
String (x): Universal (implicit processing)
typeof (obj): Used to determine the type of data
X.valueof (): Can only get the internal value of a variable for type conversion
Arbitrary type to Digital
Number (x):(automatically converted to pure digits only)
Special:
Null 0
"' 0
True
For strings--numbers
parseint (x)
From left to right, read the number string in x until you hit
The first non-numeric string exits
Unrecognized decimal part
Automatically skips empty strings
parsefloat (x)
Identify the decimal
Convert a string to a numeric value
Number and parsexxx;
The previous one is omnipotent, the latter is a string
If there is a position at the end of the string, use the latter
Any type of turn Boolean
In addition to 0 nan,null,undefined, '-->false;
Others are true
Arithmetic operator: +-*/% + +--
% seek remainder
Divide first, then take apart the endless part of the remainder
10%3=1
Step: 1:10/3=3;
2: Remainder is 1
when to use:
to determine whether a number is divisible by the
for example ,
4,5,6,7,8,9,10%2==0
divisible by 2 is divisible by 2 if the remainder is 0
limit the upper limit of the run result
such as I want to limit 3 to
3,4,5,6,6,7,8/3
+ +-(self-increment 1, auto minus 1)
Save the result to a variable
if the + +,--is used in a program called a standalone line
i++ ++i is the same
if the join operation is as follows
++i return the new value
i++ return the old value
var S=1;
Console.log (s++ + s++ + s++ + s++)
1 2 3 4
If it is an old value, the first bit returns 1, starting with the 2nd bit and 1
If it is a new value, add 1 directly from the 1th bit, as follows
Console.log (+ + S + ++s + ++s)
2 3 4
Console.log (s++ + ++s + ++s + s++)
1+ (1+1+1) (3+1) (4+1)
a++ + a++ + ++a + ++a
(3+1) + (4+1)
var a=1;
Console.log (a++ + ++a + a++ + a++ + ++a);
1+3+4+3+6=10+7
My idea is to calculate the same as a++ first, then calculate the ++a.
Finally, combine the 2
If the previous value has been added, the continuation of the accumulated record will be given to the next +1, and so on
Relationship run: do a comparison! Returns a Boolean type of false true
> < >= <=! = = equals = = = Equal!== full range
Nan is not equal to, not greater than, no less than any value (including itself)
Judge isNaN (x) If it is Nan, return true otherwise false
Do! = Comparison with any data is always true
Null: Actively emptying a variable (memory)
Undefined: Default values for all variables
= = Cannot judge null,undefined
The reason is = = = auto-Convert '
= = = can be resolved without automatic conversion
Relational operators
> < <= = = = = = = = =!==
Conclusion of the synthesis of multiple relationship runs
Return true/false BOOL when making a judgment
&& true if all conditions are true
|| True if one of the conditions is true
! Take counter
JS Note 01