This article summarizes the basic knowledge of JS, including basic syntax, data types and variables, operations, strings, arrays, objects, conditional judgments, loops, Map and Set, iterable and other knowledge points. See below for details
(1) Expansion
Reference: First, let’s talk about JS references, two ways: embedded and external calls
Editor: Sublime Text and WebStorm, PyCharm recommended
(2) Basic grammar
①The colon ends with {}
The syntax is similar to the Java language, each statement ends with ;, and the statement block uses {...}
Although JavaScript does not require adding ; at the end of each statement (the browser engine will automatically add it), it is recommended not to omit it during the development process. Because letting the browser engine automatically add semicolons will change the semantics of the program in some cases, resulting in inconsistent results with expectations
②Indent
Note that statements within curly braces {...} are indented, usually 4 spaces. Indentation is not required by JavaScript syntax, but indentation helps us understand the level of the code, so we must follow the indentation rules when writing code. Many text editors have an "auto-indent" feature (such as WebStorm's Ctrl+Alt+L) to help organize code
③ Notes
Line comments and block comments (comments are for developers to see and are automatically ignored by the JavaScript engine)
Line Comments: Characters starting with // until the end of the line are considered line comments
// this is a line comment
Block comments: wrap multi-line characters with /*...*/ to treat a large "block" as a comment
/* From here are block comments still a note still a note end of comment */
④ upper and lower case
JavaScript is strictly case-sensitive. If the case is wrong, the program will report an error or not work properly
(3) Data types and variables
①Data type
Divided into two categories: simple data and complex data
There are five kinds of simple data: undefined, null, boolean, number and string; there is only one kind of complex data, namely object.
Simple data types:
[1] undefined : Indicates that the variable does not contain a value;
[2] null : Indicates that the variable is empty;
[3] boolean : true or false represents a boolean value;
[4] number : js has only one type of number, the number can be with or without a decimal point
[5] srting : 字符串是字符的载体,而且必须被成对的引号包围
注意:typeof是javascript提供的运算符,运行后会返回括号之中数据的数据类型
如 typeof(3) 返回结果是 'number',则'hello'的类型是数字
(4)运算
①常用的运算符号
算术运算符:+ 加号,- 减号,/ 除号,* 乘号,% 求余,++ 累加,-- 递减
逻辑运算符:&&与,||或,!非
比较运算符:== ,===, != , > , < , >= , <=
注意:
①JavaScript在设计时,有两种比较运算符:
第一种是==比较,它会自动转换数据类型再比较,很多时候,会得到非常诡异的结果;
第二种是===比较,它不会自动转换数据类型,如果数据类型不一致,返回false,如果一致,再比较
例如:
if('1'==1){ alert('相同') }else{ alert('不同') }
因为用了==去做判断,所以会转换数据类型,即这里会提示‘相同’
建议:由于JavaScript这个设计缺陷,建议不要使用==比较,始终坚持使用===比较
②NaN这个特殊的Number与所有其他值都不相等,包括它自己。具体详解我在后面文章JavaScript中NAN详解里做了总结
(7)字符串
.