notes
Single line comment ctrl + / (VSCode)
//
Multiline comment Shift + alt + a(VSCode)
/**/
There are keyboard shortcut settings in the lower right corner of VSCode - > Enter default shortcut - > double click to change
JS input / output statement
method | explain | ascription |
---|---|---|
alert(msg) | Browser pop-up alert box | browser |
console.log(msg) | Browser console printout information | browser |
prompt(info) | The browser pops up an input box, which can be entered by the user | browser |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> //This is an input box prompt('Please enter your age'); //Alert pop up alert box alert('Age input is correct'); // Console console output to programmer for testing console.log('Press F12,console What can be seen in'); </script> </head> <body> </body> </html>
variable
Declare variable
var age; //Declare a variable named age
About var
-
var is a JS keyword, which is the word (the abbreviation of variable).
-
After using this keyword to declare a variable, the computer will automatically allocate memory space > for the variable, which does not need to be managed by the programmer.
-
age is the variable name defined by the programmer. We need to access the allocated space in memory through the variable name.
assignment
age = 10; //Consistent with java
Output variable
<script> var myname = "Taotao"; var age = 12; console.log(myname); console.log(age); </script>
prompt gets the variable
<script> var name = prompt('Please enter your name' + name); alert('Your name is' + name); </script>
Declare multiple variables at the same time
<script> var name = 'Liu Hongtao',age = 10,address = 'train station'; </script>
Variable defaults
var age;
- If var is not assigned as undefined
Direct use without declaration
console.log(age);
- age not defind
Do not declare direct assignment (not recommended)
age = 100; console.loog(age); //Can be used, but not recommended
Variable name specification
Like Java, it is no longer described
Java identifier, package name and naming specification (emphasis)_ Ghost Knight blog - CSDN blog
data type
Data type classification
Simple data type
-
Number
-
String
-
Boolean
-
Undefined
var a; //Variable a is declared but no value is given. At this time, a = undefined
- Null
var a = null; //Declared that variable a is null
Complex data type
- object
Numeric Number
Numeric type in hexadecimal representation
<script> //Octal, preceded by "0" var num1 = 010; console.log(num1); //8 //Hexadecimal, preceded by "0x" var num2 = 0x12 console.log(num2); //18 </script>
Numeric maximum value and minimum value
<script> console.log(Number.MAX_VALUE); console.log(Number.MIN_VALUE); </script>
Infinity
- Infinity
console.log(Number.MAX_VALUE * 2);
Infinitesimal
- -Infinity
console.log(Number.MIN_VALUE * 2);
Non numerical
- NaN
console.log('Taotao' - 100);
Judgment numerical isNaN() method
- If it is a number, it returns false; otherwise, it returns true
<script> var a = 1; var b = isNaN(a); console.log(b); </script> //false
String string (single quotation mark is recommended)
- Single and double quotation marks are supported
String nesting
var info = 'I am"Taotao"'; console.log(info);
String wrap
Similar to java, it will not be described
Get string length
var info = 'I am"Taotao"'; console.log(info.length);
String splicing "+"
var name = 'Hong'; console.log('Liu' + name + 'Waves');
The details are similar to java and will not be described
Boolean type
true
- The default value is 1
console.log(true + 1); //2
false
- The default value is 0
console.log(false + 1); //1
Difference between Undefined type and Null type
- A variable that has not been assigned a value after declaration will have a default value of undefined (pay attention to the result when connecting or adding)
var variable = undefined; console.log(variable + 'pink'); //undefinepink console.log(variable + 1); //NaN
var space = null; console.log(space + 'pink'); //Nullpink console.log(space + 1); //
Get data type method
typeof
console.log(typeof 1); //number console.log(typeof null); //object
typeof details
var age = prompt('Please enter your age'); console.log(age); //18 console.log(typeof age); //string
Console color and type
number in blue
Black is string
Light blue is boolean
Literal
-
Literal is the representation of a fixed value in the source code
-
Generally speaking, literal quantity indicates how to express this value
Numeric type conversion
Convert to string type
mode | explain | case |
---|---|---|
toString() | Convert to string | var num = 1;alert(num.toString()); |
String() cast | Convert to string | var num = 1;alert(String(num)); |
Plus concatenated string | And string splicing results are strings | var num = 1;alert(num + "I am a string"); |
Convert to digital (key)
mode | explain | case |
---|---|---|
parseInt (string) function | Convert string type to integer numeric type | parseInt('78') |
parseFloat(string) function | Convert string type to floating-point numeric type | parseFloat('78.21') |
Number() cast function | Convert string type to numeric type | Number('12') |
js implicit conversion (- * /) | Implicit conversion to numerical type by arithmetic operation | '12' - 0 |
details
console.log(parseInt('3.9')); //3 console.log(parseInt('120px')); //120 console.log(parseInt('r120px')); //NaN
Implicit conversion
console.log('12' - 0); //12 console.log('122' - '120'); //2 console.log('122' * 1); //122
Convert to Boolean
Boolean() function
console.log(Boolean('true')); //true console.log(Boolean(0)); //false