Hello, I’m Ken 🦁
Some pictures or content in the blog may come from the Internet, if there is any infringement or problems, please send me a private message
When asked how to learn JavaScript, aken 🦁 decided to contribute his notes from learning JavaScript before.
Directory portal:
2.1_ Data type
2.1.1_ Data types of variables
2.1.2_ Data type classification
2.1.3 _ digital type
The value is a string
2.1.5 _ the Boolean
2.1.6 _undefined and null
2.1.7_ Data type detection
2.1.8 _ literals
2.2_ Data type conversion
2.3 _ operator
2.4_ Branching structure
📚2.1_ Data type
📓2.1.1_ Data type of the variable
JavaScript is a weakly typed language and does not declare the data types of variables in advance. Compared to C and JAVA, these two are strongly typed languages.
// Strong typed language (Java)
int num = 10; // The variable num is an int
Copy the code
// Weakly typed languages (JavaScript)
var num = 10; // The variable num is originally a number
num = 'abc'; // Assign a string. Num is now a string
Copy the code
📒2.1.2_ Data type classification
(1) Number, containing integer values and floating point values:
var num1 = 21; / / integer value
var num2 = 0.21; // Floating-point values
Copy the code
(2) Boolean (Boolean), containing the Boolean values true and false:
var bool1 = true; // indicates true, 1, true
var bool2 = false; // Indicates false, 0, and false
Copy the code
(3) String, enclosed in single or double quotation marks:
var str1 = ' '; // Empty string
var str2 = 'abc'; // String wrapped in single quotes
var str3 = "abc"; // String wrapped in double quotes
Copy the code
(4) Undefined type (Undefined), only one value (Undefined) :
var a; // Declare the variable a with no value assigned, where a is undefined
var b = undefined; // The value of variable b is undefined
Copy the code
(5) Stereotypes (Null), only one value is Null:
var a = null; // The value of the variable a is null
Copy the code
Note that the values true, false, undefined, and null in the code are all written in lowercase.
📕 2.1.3 _ digital type
1. Octal numbers are denoted by adding a 0 to the beginning of digits in the base (1)
var num1 = 07;
console.log (num1); // Output: 7
var num2 = 010;
console.log (num2); // Output: 8
Copy the code
(2) Add 0x to the beginning of a number to indicate a hexadecimal number
var num1 = 0X9;
console.log (num1); // Output: 9
var num2 = 0Xa;
console.log (num2); // Output: 10
// The hexadecimal values "X" and "a to f" are case insensitive
Copy the code
2. Maximum Value The maximum and minimum numeric values are obtained using the following code.
console.log (Number.MAX_VALUE); // output: 1.7976931348623157e+308
console.log (Number.MIN_VALUE) ; // Output: 5e-324
Copy the code
In the output, you use scientific notation, which you can use in JavaScript to represent numbers.
3. Special values
The numeric type has three special values, Infinity, -infinity, and NaN(Not a Number)
console.log (Number.MAX VALUE * 2); // Output result: Infinity
console.log (-Number.MAX VALUE * 2);// Output: -infinity
console.log('abe' - 100); // Output result: NaN
Copy the code
The NaN attribute is a special value that represents a non-numeric value. This property is used to indicate that a value is not a number. You can set the Number object to this value to indicate that it is not a numeric value.
To determine if a variable is of a non-numeric type, use isNaN(), which returns a Boolean value with true for non-numeric and false for numeric. The example code looks like this.
console.log (isNaN(12)); // Output result: false
console.log (isNaN('abc')); // Output: true
Copy the code
📘2.1.4_ The value is a string
A string is a series of characters used to represent text in a computer. In JavaScript, a string is wrapped in single or double quotes.
var str1 = 'Single quoted string';
var str2 = "Double quoted string";
Copy the code
1. Nested single and double quotes
You can write double quotes in a single quoted string or single quotes in a double quoted string. The example code is as follows:
// Correct grammar
var strl = 'I am a "progranmer"'; // I am a"prograrmer";
var str2 = "I'm a 'prograrmer'"; // I'm a 'programmer';
// Common error syntax
var str1 = 'I'm a programmer'; Var str2 = "I ";m a "prograrmer""; Var str3 = 'I am a programmer ';; // Mix single and double quotation marks
Copy the code
2. Escape characters
When special characters such as newline and Tab are used in a string, you can use escape characters to escape them. Escape characters all start with “\”.
Escape character | explain |
---|---|
\ ‘ | Single quotes |
\n | A newline |
\ ” | Double quotation marks |
3. The value is a string length
var str1 = 'I\'m a programmer';
console.log(str1.length); // Output: 16
// I'm a programmer; // I'm a programmer
var str2 = 'I'm a programmer';
console.log(str2.length); // Output: 5
Copy the code
4. Access the characters in the string
A string can be accessed in terms of index using the “[index]” syntax. Index starts at 0 and is the length of the string minus 1. If the index maximum is exceeded, undefined is returned.
var str = 'I\'m a programmer' ; // I'm a programmer
console.log(str[0]); // Output result :I
console.log(str[1]); // Output: '
console.log(str[15]); // Output result: r
console.log(str[16]); // The output is undefined
Copy the code
5. Case: Output age
// A dialog box is displayed asking the user to enter the age
var age = prompt('Please enter your age');
// Combine statements and output
var msg = 'You this year' + age + 'old';
alert(msg);
Copy the code
📙 2.1.5 _ the Boolean
Booleans have two values: true and false
When Boolean (true false) is added to numeric (1, 0)
📗 2.1.6 _undefined and null
If a variable is declared and no value is assigned, then the value of the variable is undefined. We can also assign a null value to a variable. Null is usually used to refer to an empty object pointer, as described in a later section.
Example: Demonstrates the use of undefined and null.
var a;
console.log (a); // The output is undefined
console.log (a + '_'); // Output: undefined_(string)
console.log (a + 1); // Output result: NaN
var b = null;
console.log (b + '_'); // Output: null_(string)
console.log(b + 1); // Output :1(b = 0)
console.log(b + true); // Output :1(b = 0, true = 1)
Copy the code
📔2.1.7_ Data type detection
In development, when it is uncertain what data type a variable is, the Typeof operator can be used for data type detection.
Case study:
console.log (typeof 12); // Output result: number
console.log (typeof '12'); // Output result: string
console.log (typeof true); // Output result: Boolean
console.log (typeof undefined); // The output is undefined
console.log (typeof null); // Output result: object
Copy the code
🗒 2.1.8 _ literals
A literal is a representation of a fixed value in the source code. Simply put, a literal is used to indicate how the value should be expressed in the code.
📚2.2_ Data type conversion
📓2.2.1. Convert to a string
var num = 3.14;
// Method one uses + "" to concatenate strings
var str = num + ' ';
console.log(str, typeof str); / / 3.14 string
// Method 2 uses toString() to convert to a string
var str = num.toString();
console.log(str, typeof str); / / 3.14 string
// Mode 3 uses String() to convert to a String
var str = String(num);
console.log(str, typeof str); / / 3.14 string
Copy the code
> Note: null and undefined cannot be converted using toString() > num = 5; Num.tostring (2) converts 5 to binary, resulting in 101
var num = 5;
console.log ( num.toString(2));/ / 101
Copy the code
📒2.2.2. Convert to alphanumeric
// The first method uses parseInt() to convert a string to an integer
console.log ( parsenInt('78'));/ / 78
// Method 2 uses parseFloat() to convert a string to a floating point number
console.log ( parseFloat('3.94'));/ / 3.94
// Mode 3 uses Number() to convert the string to a Number
console.log ( Number('3.94'));/ / 3.94
// Mode 4 implicitly converts using the arithmetic operators (-, *, \)
console.log ('12' - 1); / / 11
Copy the code
To transfer data | Number() and implicit conversion | parseInt() | parseFloat() |
---|---|---|---|
Numeric string | Convert to the corresponding number | Convert to the corresponding number | Convert to the corresponding number |
An empty string | 0 | NaN | NaN |
A string beginning with a number | NaN | Convert to the beginning number | Convert to the beginning number |
A string that does not start with a number | NaN | NaN | NaN |
null | 0 | NaN | NaN |
undefined | 0 | NaN | NaN |
false | 0 | NaN | NaN |
true | 1 | NaN | NaN |
console.log ( parseInt('F'.16));// This code converts the character F to a hexadecimal number, resulting in 15
Copy the code
📕2.2.3. Convert to a Boolean
Convert to Boolean using Boolean()
console.log ( Boolean(' '));// false
Copy the code
To transfer data | Boolean() |
---|---|
0 | false |
NaN | false |
null | false |
undefined | false |
“White” | true |
12 | true |
2.3 _ 📚 operator
📓2.3.1. The arithmetic operator \
+、 -、 *、 /、 %
📒2.3.2. Increment (++) and decrement (–) operators \
The _ increment and decrement operators can be written either before or after variables (num++, num–). When placed before a variable, it is called pre-increment (decrement) and when placed after a variable, it is called post-increment (decrement). The difference between pre-increment and post-increment operators is that pre-increment returns the result of a calculation, and post-increment returns the result of a calculation.
var a = 1, b = 1;
console.log (++a); / / 2
console.log (a); / / 2
console.log (b++); / / 1
console.log (b); / / 2
Copy the code
var a = 10;
var b = ++a + 2; // b = 11 + 2, a = 11
var c = b++ + 2; // c = 13 + 2, b = 14
var d = c++ + ++a; // d = 15 + 12, c = 16, a = 12
Copy the code
Case: Demonstration,
// n , n1 = 10
var n = n1++; // n1 = 11 n1++ = 10
console.log ('n = ' + n); // n = 10
console.log ('n1 = ' + n1); // n1 = 11
n = ++n1; // n1 = 12 ++n1 = 12
console.log ('n = ' + n); // n = 12
console.log ('n1 = ' + n1); // n1 = 12
Copy the code
📕2.3.3. The comparison operator
The operator | name |
---|---|
< > > = < = | Less than, greater than, greater than or equal to, less than or equal to |
= = | Is equal to the |
! = | Is not equal to |
= = = | Are congruent |
! = = | Not congruent |
console.log (6= =6); // true
console.log (6= ='6'); // true
console.log (6= = =6); // true
console.log (6= = ='6'); // false
console.log (5! = ='5'); // true
Copy the code
\
📘2.3.4. Bitwise operators
The operator | The name of the | The sample | Computing parse |
---|---|---|---|
& | with | a&b | If both bits are 1, then 0 is 0 |
| | or | a|b | If either of the binary bits has a value of 1, the result of the operation is 1 |
~ | non | ~ a | 0 is 1,1 is 0 |
^ | Exclusive or | a^b | The binary bits are 0 if they are the same, and 1 if they are different |
<< | Shift to the left | a<<b | When we move a to the left by b, the left space will be filled with 0, and the left space will be eliminated |
>> | Moves to the right | a>>b | When a is shifted to the right by b, the left space is filled with either 0 or 1 according to the sign bit of the original number. The original negative number is filled with 1, and the positive number is filled with 0 |
>>> | Unsigned right shift | a>>>b | Move a to the right of b, discard the removed bit, and fill the highest bit on the left with 0, regardless of the original number |
// 1. The bit operator ~
var num = 7;varNum2 = ~ num; alert(num2);/ / - 8
var num3 = -1;varNum4 = ~ num3; alert(num4);/ / 0
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// The underlying principle:
// Start with the basics
// 1. The numbers in the computer are expressed in the form of complement.
// 2. If the value is inverted, 0 becomes 1.
// 3. The first number is a sign bit. 0 is a positive number and 1 is a negative number.
// 4. The original code, the inverse code and the complement of the positive number are the same.
// 5, negative numbers: the sign bit is unchanged, the rest is inverted. Complement: add 1 to the inverse code.
// The inverse complement of 6 and 0 is 0.
// ~ is the inverse of the current number. In the computer, the number is expressed in the form of complement. The number of 7 is the complement of -8, so ~7 == -8;
Copy the code
/ / use:
// select a random integer~ ~ (Math.random()*7); // Select a random integer from 0 to 6, including 0 and 6~ (Math.random()*7); // Select a random integer from -7 to -1, including -7 and -1~ (Math.random()*7); // Select a random integer from 1 to 7, including 1 and 7
//2), solve abstract leakage
// Abstract leakage: Exposing the details of the underlying implementation in the code.
// For indexOf, check whether indexOf exists, like >-1,! Details such as =-1, >=0, and <0 should be excluded
// It is easy to use ~
var str = 'hello World';if(~str.indexOf('ell'))return true;else
return false;
// 2, by bit and &
var num1 = 25 & 7;var num2 = 25 & -7;var num3 = -25 & 7;var num4 = -25 & -7; alert(num1);/ / 1alert(num2);/ / 25alert(num3);/ / 7alert(num4);/ / - 31Principle:1&1=1,1&0=0,0&1=0,0&0=0. Processing complement according to the rules:25 & 7
/ / 25 & - 7
/ / - 25 & 7
/ / - 25 & - 7
// Red & red == green Green == the complement of the last line
/ / 3, bitwise or |
/ / principle: 1 | 1 | 0 = 1 = 1, 1, 0 | 1 = 1, 0 | 0 = 0.
// Example: refer to the above.
// 4. ^ by bit
// Principle: 1^1=0, 1^0=1, 0^1=1, 0^0=0.
// Example: refer to the above.
Copy the code
// 5, left <<, right >>, unsigned right >>
// Simple notation:(a << b) ===Math.ceil(a*(2* * b)); (a >> b) ===Math.floor(a/(2**b));
Copy the code
📚2.4_ Branch structure
📓 against 2.4.1. If statements
📒 2.4.2. If… Else statements
📕 2.4.3. If… Else if statement
📘 2.4.4. A switch statement
switch(Expression){case 值1Code segment1;
break;
case 值2Code segment2;
break; .default; Code segment n; }Copy the code
Thanks for your support!