1. To view the contents of a variable, enter it in the console console.log(a);
, and the value displayed after carriage return is the contents of the variable.
2. Alert (' I don't want to execute '); Prompt box Google browser can be directly executed
3, JavaScript does not distinguish between integers and floating-point numbers, unified with number, the following are valid number types:
123; // 整数1230.456; // 浮点数0.4561.2345e3; // 科学计数法表示1.2345x1000,等同于1234.5-99; // 负数NaN; // NaN表示Not a Number,当无法计算结果时用NaN表示Infinity; // Infinity表示无限大,当数值超过了JavaScript的Number所能表示的最大值时,就表示为Infinity
Because the computer uses binary, it is sometimes convenient to use hexadecimal notation for integers, hexadecimal is represented by 0x prefixes and 0-9,a-f, for example:,, and 0xff00
0xa5b4c3d2
so on, and they are exactly the same as decimal values.
Note that %
the remainder operation is calculated.
4. String
Strings are arbitrary text enclosed in single quotes ' or double quotes , for example, and ‘abc‘
"xyz"
so on. Note that ‘‘
or ""
itself is just a representation, not part of a string , so the string is ‘abc‘
only a
, b
c
this 3 characters.
5. Comparison
In fact, JavaScript allows comparison of arbitrary data types:
false == 0; // truefalse === 0; // false
Pay special attention to the equality operator ==
. At design time, JavaScript has two comparison operators:
The first is the ==
comparison, which automatically converts the data type to compare, and many times it gets very weird results;
The second is the ===
comparison, which does not automatically convert the data type if the data type is inconsistent, returns false
if consistent, and then compares.
Because of this design flaw in JavaScript, do not use ==
comparisons, always insist on using ===
comparisons.
Another exception is that NaN
this particular number is not equal to all other values, including itself:
NaN === NaN; // false
The only way to judge NaN
is through the isNaN()
function:
isNaN(NaN); // true
Finally, pay attention to the equal comparison of floating-point numbers:
1 / 3 === (1 - 2 / 3); // false
This is not a design flaw in JavaScript. Floating-point numbers generate errors during operation because the computer cannot accurately represent infinite loop decimals. To compare two floating-point numbers for equality, you can only calculate the absolute value of their difference to see if they are less than a certain threshold:Math.abs(1 / 3 - (1 - 2 / 3)) < 0.0000001; // tru
6、
Array
An array is a set of sequentially arranged collections, each of which is called an element. An array of JavaScript can include any data type.
var arr = [1, 2, 3.14, ‘Hello‘, null, true];arr[0]; // 返回索引为0的元素,即1arr[5]; // 返回索引为5的元素,即truearr[6]; // 索引超出了范围,返回undefined
7. Objects
A JavaScript object is a set of unordered collections of key-values, such as
var person = { name: ‘Bob‘, age: 20, tags: [‘js‘, ‘web‘, ‘mobile‘], city: ‘Beijing‘, hasCar: true, zipcode: null};
The key of a JavaScript object is a string type, and the value can be any data type. All of the above person
objects define a total of 6 key-value pairs, each of which is also called an object's properties, for example, the property is person
name
‘Bob‘
zipcode
null
.
To get the properties of an object, we use 对象变量.属性名
the following method:
// ‘Bob‘person.zipcode; // null
8. Variables
The concept of variables is basically the same as the equation variables in junior algebra, but in computer programs, variables can be not only numbers, but also arbitrary data types.
Variables are represented in JavaScript by a variable name, which is a combination of uppercase and lowercase English, numeric, and numeric, and $
_
cannot begin with a number. The variable name cannot be a JavaScript keyword, such as if
, and while
so on. Declare a variable with a var
statement, such as:
var a; // 申明了变量a,此时a的值为undefinedvar $b = 1; // 申明了变量$b,同时给$b赋值,此时$b的值为1var s_007 = ‘007‘; // s_007是一个字符串var Answer = true; // Answer是一个布尔值truevar t = null; // t的值是null
Variable names can also be in Chinese, but please don't bother yourself.
In JavaScript, variables are assigned using an equal sign =
. Any data type can be assigned to a variable, the same variable can be repeatedly assigned, and can be different types of variables, but note that only var
one time, for example:
var a = 123; // a的值是整数123a = ‘ABC‘; // a变为字符串
This type of variable itself is called Dynamic language, which corresponds to static language. Static languages must specify the variable type when defining the variable, and if the type does not match, an error is given. For example, Java is a static language, and the assignment statement is as follows:
int a = 123; // a是整数类型变量,类型用int申明a = "ABC"; // 错误:不能把字符串赋给整型变量
This is why dynamic languages are more flexible than static languages.
Do not equate an equal sign of an assignment statement with a mathematical equal sign. For example, the following code:
var x = 10;x = x + 2;
If mathematically understood x = x + 2
that is not tenable in any case, in the program, the assignment statement evaluates the right expression first x + 2
, obtains the result 12
, and assigns the variable x
. Since x
the previous value is 10
, the value becomes after the value is re-assigned x
12
.
9. Strict mode
JavaScript at the beginning of the design, in order to facilitate learning for beginners, does not require the use of var
declaration variables. This design error has serious consequences: if a variable is used without a var
declaration, the variable is automatically declared as a global variable:
A var
variable that uses a declaration is not a global variable, its scope is confined to the body of the declared function of the variable (the concept of the function will be explained later), and the variable of the same name does not conflict with each other in the function body.
In order to fix the serious design flaw of JavaScript, ECMA introduced the strict mode in the follow-up specification, the JavaScript code that runs in strict mode, which is enforced by var
declaring variables, not using var
declaration variables, Will cause a run error.
The way to enable strict mode is to write on the first line of the JavaScript code:
‘use strict‘;
10. The discussion area complements://此处要在实战中弄明白let,因为我没用过
const constant Let variable, block scope, cannot repeat declaration overwrite var variable, function scope, can repeat declaration overwrite
The assignment will not be modified after the use of const, if the back will be modified to use let, do not recommend the use of Var
javascript--Liaoche Teacher Study notes (i)