1. Basic Use
1.1 Use
1. Internal Label
2. External labels
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <!-- script Write code--> <script> //<!--Pop-up window--> alert("hello world"); </script> <!--External introduction--> <script src="js/first.js"></script> </head> <body> </body> </html>
2. Introduction to Basic Grammar
1. Variables only have var
Variable type variable name = variable value;
Variables can be defined in Chinese
2. Conditional control
3. Comments
//Single line comment/** **/Multi line comment
4. Debugging
In the console of the browser,
Console.log (variable) is printed on the browser's console.
View the source code in sources.
It can also be debugged at breakpoints. (Code can be executed by refreshing the page)
3. Data type
Numeric, Text, Graphics, Audio...
number
js does not distinguish between decimal and integer, Number
123//integer 123.1//Floating point number 1.21321e3//Scientific Counting Infinity//Infinity
Character string
"aasd"
'asd'
Boolean Value
ture,false
Logical operation
&&
||
!Reverse
Comparison operator
=equal to
==equals
===Absolutely equal (same type, same value, true)
** Do not use == **
NaN is not equal to all values, including myself
Equality can only be determined by isNaN (NaN)
There is a loss of precision in decimal operations.
Math.abs( )<0.00000001
To determine.
null and undefined
null is empty, undefined is undefined
numerical value
A series of sets of the same type, eg:
var arr = [1,2,3,null,"hello world",true]; new Array[1,2,3,null,"hello world",true];
The first is used to ensure code readability.
JavaScript has no array boundaries, only undefined.
object
For braces, eg:
var person = { name:"MCL"; age:3; tags:[1,2,3,"dasd"] }
person.name > MCL
Each property is separated by commas, and the last one does not need to be added.
(It looks like a structure here)
3. Strict check mode strict
Not declaring var as a global variable is highly nonstandard
Defining local variables with let
'use strict'; //Strict definition //idea to support ES6 //Must be written on the first line //Develop good habits
4. Data Type-String
1. Normal strings are wrapped in single and double quotes
2. Note the escape character
3. Multiline string writing (`)
var src = ` hello asd asd dsfsd `;
4. Template string
let name = "MCL"; let age = 19; let all = `nihao,${name}`;
5. String Length
console.log(str.length)
6. Variability of strings, "Invariant"
7. Case Conversion
//Note that there are many properties of the method str.toUpperCase() str.toLowerCase()
8. Get the specified subscript
src.indexof('')
9. Intercept strings
src.substring(1,4)
Include the front, not the back, [,)
5. Data Structure-Array
Array can contain any data type
1. Length
arr.length
Assigning arr.length changes the size of the array ~If it is too small, elements will be lost
2.indexOf(), get subscript index
String's "1" and 1 are different
3.slice(2,5) intercepts part of Arry, returns a new array, closes left and opens right
4.push,pop
Arr.push() is to press an element inside (tail)
pop() ejects the element from the tail
5 unshift(), shift() head
ushift() pressed into head
shift() ejects head
6.sort() sort
7. Element inversion reverse()
8 concat([1,2,3]) stitching
The original array was not modified to return a new array
9 join join join symbol
arr.join('-')
10-Dimensional Array
6. Objects
Several key-value pairs
var person ={ Property Name = Attribute Value, ..* }
Last without comma
Use a nonexistent object property, no error just undefined
Delete delete
delete person.name > true
Add, add directly
Determine whether the attribute value is in this object
xxx in xxx
6. Process Control
if judgment
if{ } if else{ } else{ }
while Loop
while() { }
for loop
for( ; ; ){ }
do-while Loop
do{ }while()
Array loop for in
for(var num in age){ }
7.Map and Set
Map key-value pairs
var map = new Map([[1,2],["asd",2340]]); map.get('tom')
Add key-value pairs
map.set(1,2)
Delete key
map.delete(1)
Set Unordered Non-repeating Set
var set = new Set([1,2,34]); set.add(4);//Add to set.delete(34);//delete