variable
var a; / / declare
a = 1; / / assignment
var b = 2; // Declare and assign
var x = 1Y, z is equal to3; // Single declaration
Copy the code
The keyword
You cannot use keywords and reserved words as variable or function names
break
case
catch
continue
default
delete
do
else
finally
for
function
if
in
instanceof
new
return
switch
this
throw
try
typeof
var
void
while
with
Copy the code
Reserved words
Some reserved words are already used in ES6 syntax
abstract
boolean
byte
char
class
const
debugger
double
enum
export
extends
final
float
goto
implements
import
int
interface
long
native
package
private
protected
public
short
static
super
synchronized
throws
transient
volatile
Copy the code
The original value
-
JavaScript has five primitive values, or five basic types
Number String Boolean undefined null Copy the code
-
Base types are automatically determined based on the values assigned after the declaration, which is called weakly typed languages
var a = 1; var str = "lean javascript"; var flag = true; var u = undefined; // undefined, equivalent to declaring without assigning var n = null; // Void, used to initialize components and destroy functions Copy the code
-
Raw values are stored in stack memory
var a = 1; // create space in stack memory where 1 points to A var b = a; // copy the value of a to create space in stack memory, pointing to b var a = 2; // Create a new stack memory space, store 2 to a, the original space waiting to be overwritten Copy the code
Reference value
-
Commonly used reference values, that is, reference types
object array function data RegExp Copy the code
-
Reference values are stored in heap memory
var arr1 = [1.2.3.4]; // [1,2,3,4] is stored in heap memory, which holds the address pointing to the array var arr2 = arr1; // arr1, arr2 both point to the heap [1,2,3,4] arr1.push(5); // push to operate on arrays in the heap console.log(arr2); // print out [1,2,3,4,5] arr1 = [1.2.3.4.5.6]; // redirects the heap to [1,2,3,4,5,6] console.log(arr2); // still print [1,2,3,4,5] Copy the code
error
-
Grammar mistakes
The code doesn’t execute in a block of code
console.log(1); // Will not be executed console.log(2);// Use Chinese semicolon, syntax error console.log(3); // Will not be executed Copy the code
-
General error
It’s going to be executed, it’s not going to be executed
console.log(1); / / console.log(a);// a undeclared, generic error reference error console.log(3); // Will not be executed Copy the code
-
If one
The operator
-
The assignment
var a = 1./ / assignment b = 2, c = 3; var d = (a + b) * d; // Declare d -> calculate -> assign // Parenthesis operation > Normal operation > assignment Copy the code
-
add
var a = 1 + 1; // 2, add var b = "str1" + "str2"; Str1str2, string concatenation var c = "str1" + 1; // string concatenation var d = "str" + null + undefined + NaN; // string + any type is string var e = 1 + 1 + "str" + (1 + 1); // 2str2 Copy the code
-
division
var a = 0 / 0; // NaN, non-number, Number type Number // Any operation with NaN yields NaN var b = "a" / "b"; // NaN var c = 1 / 0; // The Number type is Infinity Var d = -1 / 0; // -Infinity Copy the code
-
modulus
// take the mod (mod) var a = 4 % 6; / / 4 var b = 6 % a; / / 2 var c = 4 % 0; // NaN, non-number Copy the code
-
To compare
var a = 1 > 0.// true b = 1 > "0".// true, convert the string to a number before comparing c = "a" > "b".// false, based on the ASCII code d = "4.5" > "11".// true, '4' > '1' e = "1.5" > "11"; // false, '1' = '1', '.' < '1' var bool = 1= =1; // true var bool = 1= ="1"; // true, equality does not require data types var bool = 1= = ="1"; // false if the data type is congruent var bool = NaN= =NaN; // false, NaN is not equal to any value, including itself Copy the code
-
logic
/* * false value: undefined null "" 0 NaN flase */ var a = 1 && 2; / / 2 var b = 1 || 2; / / 1 // &&, if true, go back, if false or complete, return the current value / / | |, encounter the false in the future, meet true or through, returns the current value var c = !1; // false Copy the code
Under controlled conditions
-
if
var a = 65; if(a >= 60 && a <= 100) { console.log("Pass"); } else if(a >= 0 && a < 60) { console.log("Fail"); } else { console.log("Wrong score."); } // Top-down judgment, once satisfied, do not look behind Copy the code
-
switch
var a = 65; switch(true) { // Implement if with switch case a >= 60 && a <= 100: console.log("Pass"); break; case a >= 0 && a < 60: console.log("Fail"); break; default: console.log("Wrong score."); } // If no break is added, all subsequent case blocks will be executed Copy the code
cycle
-
if
for(var i = 0; i < 10; i++) { // ... } /* * 1. Declare variables * 2. Judge conditions * 3. Repeat 2, 3, 4, and once the condition is determined to be flase, end the loop */ Copy the code
Print out primes up to 100
var c = 0; for(var i = 2; i < 100; i++) { // 1 is not prime for(var j = 1; j <= i; j++) { if(i % j == 0){ c++; }}if(c == 2) { console.log(i); }}Copy the code
-
while
var i = 0; // while while( i < 10) { // ... i++; } // do... While executes a block of code once and determines whether it follows do { i++; }while(i < 10); Copy the code
typeof
-
Typeof is an operator used to determine data types
-
The use of the typeof
typeof(1); // Perform operations on expressions typeof 1; // Perform operations on variables Copy the code
-
Return value of typeof
typeof(123); // number typeof("123"); // string typeof(true) // boolean typeof(a); // undefined, a undefined typeof({}); // object typeof([]); // object typeof(new Array(1.2.3)); // object /* * The object returned here is a general object, array, a large concept */ typeof(null); // object typeof(Array) // function, understood as an object constructor Copy the code
Type conversion
-
Display type conversion
Number
Number("123"); / / 123 Number("a"); // NaN Number(true); / / 1 Number(undefined); // NaN Number(null); / / 0 Copy the code
parseInt
parseInt("123.99");// 123, the integer part parseInt("a"); // NaN parseInt(true); // NaN parseInt(undefined); // NaN parseInt(null); // NaN // The second argument, given the base parseInt(10.16); // 16 hexadecimal 10 -> 10 base 16 parseInt("b".16); // 11 hexadecimal b -> 10 base 11 // Start with a number or a plus or minus sign to extract the preceding number parseInt("abc123"); // NaN parseInt("-123abc"); / / - 123 Copy the code
parseFloat
parseFloat("3.1415"); / / 3.1415 parseFloat("3.145").toFixed(2); // round to 3.15, keep two digits // Start with a number or a plus or minus sign to extract the preceding number parseFloat("3abc"); // Copy the code
toString
parseInt("100").toString(16) // "64", string "100" -> number 100 -> hexadecimal number string Boolean("abc"); // true Boolean("abc").toString(); // "true" Copy the code
-
Implicit type conversion
The string conversion number
// String -> number console.log(+"123"); / / 123 console.log(typeof(-"123"); // number String -> number var a = "1"; a++; String -> number string -> number var c = "3" * 2; / / 6 // The ratio of numbers to strings string -> number var d = 1 > "2"; // false String -> number var e = 1= ="1"; // true var f = 1! ="2"; // true // Whether numbers and strings are congruent is not converted var e = 1= = ="1"; // false Copy the code
Number converted string
// String stitching number -> string var b = "str" + 1; // str1 Copy the code
Boolean conversion number
// Boolean is implicitly converted to a number var g = 2 > 1 = 1; // true, 2 > 1 -> true -> 1, 1 = 1 -> true Copy the code
Undefined and null
/ / undefined, null var h = undefined > 0.// false i = undefined < 0.// false j = undefined= =0; // false // Undefined and 0 both return false, as does null var k = undefined + undefined; // NaN var l = null + null; / / 0 var m = null + undefined; // NaN var o = undefined= =null; // true Copy the code
isNaN()
// isNaN(), first through Number() to determine if the result is a NaN isNaN("123"); // false isNaN("123abc"); // true isNaN(null); False, null -> 0 isNaN(undefined); // undefined -> NaN Copy the code
ASCII and Unicode
-
ASCII
- Table 1 0-127 and Table 2 128-255
- One byte per character
-
Unicode
- The first 255 bits are ASCII, one byte for each character
- 256 bits and thereafter, each character is two bytes
-
Get Unicode encoding
// String method charCodeAt(index) "a".charCodeAt(0); // a -> 97 Copy the code