Introduction to JavaScript Basics 02

Table of contents

1. Use of variables

1.1 Basic usage

1.2 Understanding dynamic types

2. Basic data types

2.1number number type

2.2 Number representation

2.3 Special numerical values

2.4string string type

2.4.1 Basic rules

2.4.2 Escape characters

2.4.3 Find the length

2.4.4 String concatenation

2.5boolean Boolean type

2.6undefined undefined data type

2.7null null value type

3.Operator

3.1 Arithmetic operators

3.2 Assignment operator & compound assignment operator

3.3 Increment and decrement operators

3.4 Comparison operators

3.5 Logical operators

3.6 Shift operation


1. Use of variables

1.1 Basic usage

Create variables (variable definition/variable declaration/variable initialization)

var name = 'zhangsan';
var age = 20;

var is a keyword in JS, indicating that this is a variable.
= means "assignment" in JS, which is equivalent to putting data into a box in memory. = It is recommended to have a space on both sides
Each statement should have a; at the end. It can be omitted in JS; but it is recommended to add it.
Note that here 's; is an English semicolon. All punctuation marks in JS are English punctuation marks.
If the initialized value is a string, it must be enclosed in single quotes or double quotes.
If the initialized value is a number, then you can assign it directly.

use variables

console.log(age); // 读取变量内容
age = 30;     // 修改变量内容

Why do characters in anime have to shout out the name of a skill before actually releasing it?
It’s because variables must be declared before they can be used.

Code example: A pop-up box prompts the user to enter information, and then the pop-up box displays.

var name = prompt("请输入姓名:");
var age = prompt("请输入年龄:");
var score = prompt("请输入分数");
alert("您的姓名是: " + name);
alert("您的年龄是: " + age);
alert("您的分数是: " + score);

You can also merge the three output contents into one pop-up box

var name = prompt("请输入姓名:");
var age = prompt("请输入年龄:");
var score = prompt("请输入分数");
alert("您的姓名是: " + name + "\n" + "您的年龄是: " + age + "\n" + "您的分数是: " +
score + "\n");

+ means string concatenation, that is, concatenating two strings end to end into one string.
\n means line break
JavaScript It also supports the use of let to define variables. The usage is basically similar to that of var. The differences in usage will not be discussed here for the time being.

1.2 Understanding dynamic types

1) The variable type of JS is determined during the running of the program (the type will not be determined until the = statement is run)

var a = 10;   // 数字
var b = "hehe"; // 字符串

2) As the program runs, the type of variables may change.

var a = 10;   // 数字
a = "hehe";   // 字符串

This is quite different from statically typed languages ​​such as C and Java.
C, C++, Java, Go and other languages ​​are statically typed languages. The type of a variable is determined when it is created. OK,
cannot be changed during runtime.
If you try to change, an error will be reported during compilation.

2. Basic data types

Several types built into JS
number: Number. Does not distinguish between integers and decimals.
boolean: true, false. a> null: only unique value The value null. Indicates an empty value. undefined: only unique value undefined. Indicates undefined value.
string: string type.

2.1number number type

JS does not distinguish between integers and floating point numbers, and they are all represented by "numeric types"

2.2 Number representation

Computers always use binary to represent data, while people usually use decimal.
Because binary is not convenient to use (too many 01s can be confusing).
Therefore, when using binary numbers in daily life, octal and hexadecimal are often used to represent binary numbers.

var a = 07;    // 八进制整数, 以 0 开头
var b = 0xa;   // 十六进制整数, 以 0x 开头
var c = 0b10;   // 二进制整数, 以 0b 开头

Note:
One octal number corresponds to three binary digits
One hexadecimal number corresponds to four binary digits. (Two hex A base number is a byte)
Conversion between various bases does not require manual calculation, just use a calculator.

2.3 Special numerical values

Infinity: Infinity, larger than any number. Indicates that the number has exceeded the range that JS can represent.
-Infinity: negative infinity, smaller than any number. Indicates that the number has exceeded the range that JS can represent. Represented range.
NaN: Indicates that the current result is not a number.

var max = Number.MAX_VALUE;
// 得到 Infinity
console.log(max * 2);
// 得到 -Infinity
console.log(-max * 2);
// 得到 NaN
console.log('hehe' - 10);

Note:
1. Negative infinity and infinitesimal are not the same thing. Infinite means infinitely close to 0, and the value is 1 / Infinity
2 . 'hehe' + 10 What you get is not NaN, but 'hehe10', which will implicitly convert the number into a string and then concatenate the strings.
3. You can use the isNaN function to determine whether it is a non-number.

console.log(isNaN(10));  // false
console.log(isNaN('hehe' - 10));  // true

2.4string string type

2.4.1 Basic rules

String literals need to be enclosed in quotes, either single or double quotes are acceptable.

var a = "haha";
var b = 'hehe';
var c = hehe;   // 运行出错

What if the string already contains quotation marks?

var msg = "My name is "zhangsan"";   // 出错
var msg = "My name is \"zhangsan\"";  // 正确, 使用转义字符. \" 来表示字符串内部的引
号.
var msg = "My name is 'zhangsan'";   // 正确, 搭配使用单双引号
var msg = 'My name is "zhangsan"';   // 正确, 搭配使用单双引号

2.4.2 Escape characters

Some characters are inconvenient to input directly, so they need to be represented in some special ways.
\n
\\
\'
\"
\t

2.4.3 Find the length

Just use the length property of String

var a = 'hehe';
console.log(a.length);
var b = '哈哈';
console.log(b.length);

2.4.4 String concatenation

Use + to splice

var a = "my name is ";
var b = "zhangsan";
console.log(a + b);

Note that numbers and strings can also be concatenated

var c = "my score is ";
var d = 100;
console.log(c + d);

Note that you need to identify whether the added variables are strings or numbers.

console.log(100 + 100);   // 200
console.log('100' + 100);  // 100100

2.5boolean Boolean type

means "true" and "false"
        boolean was originally a concept in mathematics (Boolean algebra).
        In computers boolean is of great significance and is often used with conditions/loops to complete logical judgments.
Boolean is treated as 1 and 0 when participating in operations.

console.log(true + 1);
console.log(false + 1)

This kind of operation is actually unscientific. It should not be written like this in actual development.

2.6undefined undefined data type

If a variable has not been initialized, the result is undefined, which is of type undefined

var a;
console.log(a)

Add undefined and string, and the result is string concatenated.

console.log(a + "10");  // undefined10

Add undefined to a number, the result is NaN

console.log(a + 10);

2.7null null value type

null means that the current variable is a "null value".

var b = null;
console.log(b + 10);   // 10
console.log(b + "10");  // null10

Note:
Both null and undefined indicate that the value is illegal, but the emphasis is different.
null indicates that the current value is empty. ( Equivalent to an empty box)
undefined means that the current variable is undefined. (Equivalent to no box at all)

3.Operator

3.1 Arithmetic operators

+
-
*
/
%

3.2 Assignment operator & compound assignment operator

=
+=
-=
*=
/=
%=

3.3 Increment and decrement operators

++: Self-increase 1
--: Self-reduction 1

3.4 Comparison operators

<
>
<=
>=
== Comparison of equality (implicit type conversion will be performed)
!=
=== Comparison of equality (implicit type conversion will not be performed)
!==

3.5 Logical operators

Used to evaluate multiple boolean expressions.
&& and: one false, then false
|| or: one true Then true
! Not
Bitwise operation
& Bitwise AND
| Bitwise Or
~ Bitwise negation
^ Bitwise XOR

3.6 Shift operation

<< left shift
>> signed right shift (arithmetic right shift)
>>> unsigned Shift right (logical shift right)

Guess you like

Origin blog.csdn.net/qq_65307907/article/details/133754505