JavaScript It's object-based and event driven , Script language with security performance
Common misconceptions about : We usually think JavaScript And Java There's a connection , As a matter of fact ,JavaScript Except in grammar Java, The rest of it doesn't matter .
JavaScript Key points in the development process
JavaScript Implementation principle of
JavaScript Grammar and Java Language is similar to , Each statement is marked with ;
end , Use... For statement block {...}
. however ,JavaScript It is not mandatory to add... At the end of each statement ;
, The browser is responsible for executing JavaScript The engine of the code will automatically add... At the end of each statement ;
. Give Way JavaScript The engine automatically semicolons in some cases will change the semantics of the program , Results in inconsistent results with expectations
JavaScript The core syntax in consists of variables 、 data type 、 Array 、 Operation symbol 、 Control statement 、 notes 、 Input / Output 、 Grammatical conventions make up
var// Keywords used to declare variables
// Declare variables before assigning values
var x;
x=5;
// Declare and assign variables at the same time
var x=5;
// Variables can be used without declaration , But this method is easy to make mistakes
x=5;
// Variable itself type is not fixed , therefore JavaScript It's dynamic language
var s=1;
s="abc";
data type
JavaScript The data types in are :undefined null number boolean string Array Object
undefined : Variable has no initial value , It's only useful to determine if a function parameter is passed .
null : Represents a null value , And undefined The values are equal . It and 0 And empty strings '' Different ,0 It's a number ,'' The length is 0 String , and null Express “ empty ”.
number : JavaScript Don't distinguish between integers and floating-point numbers , Unified use Number Express
boolean : ture / false
string : Single quote ' Or double quotes " Any text enclosed .
Array : JavaScript The array of can include any data type
Object : JavaScript Object is a set of keys - An unordered set of values , Keys are all string types , Value willing is any data type .
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var str = "JavaScript";
var num = 10;
var u;
var arr=[1,2,3,str];
var date = new Date();
// Output at console
console.log(typeof str);
console.log(typeof num);
console.log(typeof u);
console.log(typeof arr);
console.log(typeof date);
</script>
</body>
</html>
String type
JavaScript The string of is used ''
or ""
The enclosed characters indicate . If '
It's also a character , Then you can use ""
Cover up , But if the string contains both '
Contain, "
You need to use escape characters \
To mark .
Template string : To connect multiple strings , It can be used +
Connection No . If there are many variables that need to be connected , use +
The number is more troublesome ,ES6 Added a template string , use ${ Variable name } Express , Can automatically replace variables in string .
Common methods :
Array
How to create an array :
var arr = [1,2,3 ];
var arr =new Array(5);
arr[0]=1;
arr[1]=3;
var arr = new Array(1,2,3);
Array common methods :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
var str="apple,banana,orange,watermelon,peach";
var arr=str.split(",");
console.log("====== adopt split Split into arrays ========")
console.log(arr[0]);
console.log(arr[1]);
console.log(arr[2]);
console.log(arr[3]);
console.log(arr[4]);
console.log("======= adopt join Turn into string========");
var str1 = arr.join("-");
console.log(str1);
console.log("======== adopt push Additive elements ===============")
arr.push("strawburry");
console.log(arr)
</script>
</head>
<body>
</body>
</html>
Operator
&&
Operation is with operation , Only for true
,&&
The result is true
||
Operation is or is , As long as one of them is true
,||
The result is true
!
Operation is not operation , It's a monocular operator , hold true
become false
,false
become true
>
>=
<
<=
==
It should be noted that ==
Will automatically convert the data type and then compare , A lot of times , It's going to get very weird results , and ===
Data types are not automatically converted , If the data types are not consistent , return false
, If the same , Compare again .Math.abs(1 / 3 - (1 - 2 / 3)) < 0.0000001; // true