data type
undefined, null, boolean, string, symbol, number, bigint, and object
Variable assignment
var ourName = 0; Usually, when a variable is declared, an initial value is initialized to the variable ourName= 5; Assign the value of one variable to another var myNum; myNum = ourName;
When a variable is declared, the program will give it an initial value undefined
When an operation is performed on a variable with an undefined value, the calculated result will be NaN, which means "Not a Number".
When string splicing is performed with a variable whose value is undefined, it will be converted to string undefined.
Number increment + =, decrement - =, multiply * =, divide/=
increase i++; i+=1; i=i+1; reduce i--; i-=1; ride myVar *= 5; except myVar /= 5;
Remainder operator
5 % 2 = 1 because Math.floor(5 / 2) = 2 ((commercial) 2 * 2 = 4 5 - 4 = 1 (Remainder)
Escape character
Use a backslash before the escaped character(\) var sampleStr = "Alan said, \"Peter is learning JavaScript\"."; \' Single quotation mark \" Double quotation mark \\ Backslash \n Newline character \r Carriage return \t Tab \b Backspace \f Page feed
Single quotation mark double quotation mark
When multiple quotation marks are used in a string, single quotation marks can be used to wrap double quotation marks or vice versa. Escape characters are not necessary at this time
Find the length of the string
"Alan Peter".length
index
var firstName = "Charles"; var firstLetter = firstName[0]; Get last value var lastLetter = firstName[firstName.length - 1];
Modify the data in the array
var ourArray = [50,40,30]; ourArray[0] = 15;
push() adds an element to the end of the array
Add data to the end of the array
var arr1 = [1,2,3]; arr1.push(4);
. unshift() adds an element to the array header
The usage is the same as push
var ourArray = ["Stimpson", "J", "cat"]; ourArray.unshift("Happy");
. pop() removes the last element of the array
Removes the element at the end of the array and returns it
var threeArr = [1, 4, 6]; var oneDown = threeArr.pop();
. shift() removes the first element of the array
Removes the first element of the array and returns it
var ourArray = ["Stimpson", "J", ["cat"]]; var removedFromOurArray = ourArray.shift();
. Writing reusable code with functions
function testFun(param1, param2) { console.log(param1, param2); } testFun()
. Global scope and function
Variables with global scope can be called anywhere in the code
Variables that are not defined with the var keyword will be automatically created in the global scope to form global variables.
Local scope and function
function myTest() { var loc = "foo"; Visible only within this function console.log(loc); }
Global and local scopes in functions
var someVar = "Hat"; function myFun() { var someVar = "Head"; return someVar; } function myFun The string will be returned Head,Because local variables have higher priority
if statement
function test (myCondition) { if (myCondition) { return "It was true"; } return "It was false"; } test(true); test(false);
Strict equality operator===
And relative equality operator(==)Unlike, the strict equality operator does not do type conversion. 3 === 3 true 3 === '3' false
Strict inequality operator==
The data type of the value will not be converted
3 !== 3 false 3 !== '3' true 4 !== 3 true
View data type typeof
typeof '3'
And or not
&& And || or
Switch statement
Switch matches the value of the expression with the case clause
Test case values are compared using the strict equality (= = =) operator.
Break tells JavaScript to stop executing the switch statement. If break is omitted, the next statement will be executed.
switch(lowercaseLetter) { case "a": console.log("A"); break; case "b": console.log("B"); break; default: defaultStatement; break; } add to default Statement, it will not find a match case Statement. You can think of it as if/else The last one in the chain else sentence
Multiple judgment settings have the same result var result = ""; switch(val) { case 1: case 2: case 3: result = "1, 2, or 3"; break; case 4: result = "4 alone"; }
object (Dictionary)
Access object properties (Dictionary)
There are two ways to access object properties: dot notation (.) and square bracket notation ([])
var myObj = { prop1: "val1", prop2: "val2" }; var prop1val = myObj.prop1; var prop2val = myObj[prop2];
Update (add) object properties (Dictionary)
There are also two ways to update: point (.) and square brackets ([])
Delete object properties (Dictionary)
delete ourDog.bark;
Check whether the object property exists
var myObj = { top: "hat", bottom: "pants" }; myObj.hasOwnProperty("top"); true myObj.hasOwnProperty("middle"); false
Accessing nested objects
var ourStorage = { "desk": { "drawer": "stapler" }, "cabinet": { "top drawer": { "folder1": "a file", "folder2": "secrets" }, "bottom drawer": "soda" } }; ourStorage.cabinet["top drawer"].folder2; ourStorage.desk.drawer;
for loop
var ourArray = []; for (var i = 0; i < 5; i++) { ourArray.push(i); }
do... while loop
In any case, it will first run the first part of the code in the do loop, and then continue to run the loop when the condition specified by while is evaluated as true
recursion
function multiply(arr, n) { if (n <= 0) { return 1; } else { return multiply(arr, n - 1) * arr[n - 1]; } } Equivalent to: function multiply(arr, n) { var product = 1; for (var i = 0; i < n; i++) { product *= arr[i]; } return product; }
floor rounding down
Math.floor(Math.random() * 20); Round down to get the nearest integer
parseInt converts a string to an integer
var a = parseInt("007");
parseInt function
parseInt(string, radix); var a = parseInt("11", 2); variable radix Indicates that 11 is in a binary system. This example converts string 11 to integer 3
Conditional operator (ternary operator)
a ? b: c, where a is a condition. Run code b when the condition returns true and code c when the condition returns false
return a > b ? "a is greater" : "b is greater or equal";
Multilayer ternary operation
return (a === b) ? "a and b are equal" : (a > b) ? "a is greater" : "b is greater";