1. Data type and type conversion
(1) Data type and typeof()
The following basic data types are provided in JavaScript
String string
Number value
Boolean Boolean type
null empty
undefined Undefined
typeof (dat) can get the data type of the specified data
(2) Type conversion
1. Convert to numeric type
There are three fixed syntax for converting data to numeric types
Number (DAT) converts dat data to numeric values
parseInt (DAT) converts dat data to an integer
Parsefloat (DAT) converts dat data to floating point numbers
console.log(Number(5),typeof(Number(5))); console.log(Number(3.14),typeof(Number(3.14))); console.log(Number("12"),typeof(Number("12"))); console.log(Number("zhangsan"),typeof(Number("zhangsan"))); console.log(Number("12px"),typeof(Number("12px"))); console.log(Number("a12px"),typeof(Number("a12px"))); console.log(Number(false),typeof(Number(false))); console.log(Number(true),typeof(Number(true)));
2. Convert to string
There are three syntax for converting data to strings
String(dat) Convert dat data to string data
dat.toString() Convert dat data to string form
dat + “” Convert dat to string form using concatenated string form
console.log(String("zhangsan"),typeof(String("zhangsan"))); console.log(String(1223),typeof(String(1223))); console.log(String(false),typeof(String(false))); console.log("zhangsan".toString(),typeof("zhangsan".toString())); console.log((123).toString(),typeof((123).toString())); console.log((false).toString(),typeof((false).toString())); console.log("zhangsan"+"",typeof("zhangsan"+"")); console.log(123+"",typeof(123+"")); console.log(false+"",typeof(false+""));
'
3. Convert to boolean type
The fixed syntax for converting data to Boolean types is Boolean (dat)
console.log(Boolean(0),typeof(Boolean(0))); console.log(Boolean(0.0),typeof(Boolean(0.0))); console.log(Boolean(-20),typeof(Boolean(-20))); console.log(Boolean(""),typeof(Boolean(""))); console.log(Boolean(" "),typeof(Boolean(" "))); console.log(Boolean("abc"),typeof(Boolean("abc")));
4.null and undefined conversion (PIT)
Convert to numeric type (note)
console.log(Number(null),typeof(Number(null))); console.log(parseInt(null),typeof(parseInt(null))); console.log(parseFloat(null),typeof(parseFloat(null))); console.log(Number(undefined),typeof(Number(undefined))); console.log(parseInt(undefined),typeof(parseInt(undefined))); console.log(parseFloat(undefined),typeof(parseFloat(undefined)));
Convert to string
console.log(String(null),typeof(String(null))); console.log(null+"",typeof(null+"")); console.log(String(undefined),typeof(String(undefined))); console.log(undefined+"",typeof(undefined+""));
console.log((null).toString(),typeof((null).toString())); //An error will be reported here console.log((undefined).toString(),typeof((undefined).toString())); //An error will be reported here
Convert to Boolean
console.log(Boolean(null),typeof(Boolean(null))); console.log(Boolean(undefined),typeof(Boolean(undefined)));
(3) Type implicit conversion
When some code is executed, it will automatically complete the data type conversion at the bottom. This conversion method is implicit conversion
There are two common implicit conversions
1.isNaN(dat)
Function: judge whether a data is a non number
console.log(isNaN("123")); console.log(isNaN("123a"));
2.dat.toFixed(n)
Function: convert data of a numeric type into a string after specifying the number of decimal places to be reserved
console.log((3.141592654).toFixed(2),typeof((3.141592654).toFixed(2)));
2. Operator
(1) Common operators
1. Arithmetic operator
Symbol | describe | Example |
+ | Addition operator | 12+9 |
- | Subtraction operator | 12-9 |
* | Multiplication operator | 12*9 |
/ | division operator | 12/9 |
% | Remainder operator | 12%9 |
---,++ | Self decreasing and self increasing operators | a++,a-- |
2. Assignment operator
Symbol | describe | Example |
= | Assign values to variables | var a = 12 |
-= | Extended subtraction | var a-=12 => var a=a-12 |
+= | Extended addition operation | var a+=12 => var a=a+12 |
*= | Extended multiplication | var a*=12 => var a =a*12 |
/= | Extended Division | var a/ = 12 => var a=a/12 |
3. Relational operators
Symbol | describe | Example |
> | Greater than relation | 18 > 16 the result returns a Boolean value |
< | Less than relation | 18<16 |
>= | Greater than or equal to | 18.<=16 |
<= | Less than or equal to | 18 <=16 |
== | Equal value | 10 = "10" |
!= | Values are not equal | 10 != '11' |
=== | Value and type are equal [identity] | 10 === '10' |
!== | Values or types are not equal | 10 !== '10' |
4. Logical operators
Symbol | describe | Example |
&& | Judge whether both sides are true (logical and) | 10 <12 && 21 <22 |
|| | Judge whether the conditions on both sides are false (logical or) | 10<12 || 12>>13 |
! | Direct negation of results (logical non) | !(10 > 12) |
(2) Use of operators
1. Use of arithmetic operators
var a = 12; var b = 9; console.log(a+b,a-b,a*b,a/b,a%b,a++,b--);
Note: a + + means operation before self addition, and + + a means self addition before operation
var a =10; var b = 20; var c = a++ + ++a +b++; /* *Analytic c= 10 + 12 + b++ * c= 10 + 12 + 20 */ console.log(a,b,c);
2. Use of comparison operators
console.log(12>18,12<18,12>=18,12<=18,12=="12",12==="12",12!="12",12!=="12");
In addition to numbers, you can also compare strings: you compare the ascii code of strings
console.log("1000">"100","a">"b","ab" > "b","ab">"aaa");
3. Use of logical operators
console.log(10>9&&10>8,10>9&&10<8,10<9&&10<8); console.log(10>9||10>8,10>9||10<8,10<9||10<8); console.log(!(false));