本文最后更新于:2023年12月5日 下午

本文记录 node.js 最基本的语法。

数据类型

Node.js有一些核心类型:number,boolean,string,object、undefined 和 function。

可以使用 typeof 查看数据类型

number

数字

1
2
typeof(3.5)
typeof(3)

boolean

true 和 false

1
2
typeof(true)
typeof(false)

string

字符串

1
typeof('abc')

object

node.js 对象,由 {} 定义 或 new Object();

1
2
3
4
5
6
7
8
9
var user = {
first_name: "HTML",
last_name: "CSS",
age: 32,
};
var empty = new Object()

console.log(typeof(user))
console.log(typeof(empty))

undefined

意味着尚未设置或根本不存在

1
2
3
var a
typeof(a)
typeof(askfalsjdflasf)

function

函数类型

1
2
3
4
5
6
function hello(name) {
console.log("hello " + name);
}

hello('abc')
console.log(typeof(hello))

运算符表达式

算术运算符

  • 加+,减-,乘*,除 /,括号();
1
2
3
4
5
6
var a = 10; // 赋值语句
a = 4 * 23 + 188 / 6; // 数学表达式
var c = 7;
var b = (a + c) / 2; // 变量的数学表达式
console.log(a);
console.log(b);

比较运算符

  • 等于==,小于等于<=,大于等于 >=,不等于 !=
1
2
3
4
5
// 比较表达式,如果成立,那么为true,否则为false;
console.log(a > b);
console.log(a == b);
console.log(a <= b);
console.log(a != b);

逻辑运算符

  • 与&&,或 || 非 !
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//&&所有条件都满足才为真
//||所有条件中只要有一条满足就为真
console.log("&&&&&&&&&");
console.log(false && false);
console.log(false && true);
console.log(true && false);
console.log(true && true);

// 逻辑或
console.log("||||||||||||");
console.log(false || false);
console.log(false || true);
console.log(true || false);
console.log(true || true);

console.log((false && false) || true); // true
console.log(false && (false || true)); // false

// 非
console.log(!true)
console.log(!false)

字符串相关运算符

1
2
3
4
5
6
7
8
9
10
// 字符串的加法是把各个字符串连接在一起,生成一个新的字符串;
var str = "Hello i am ";
var name = "a girl"
var str_total = (str + name);
console.log(str, name, str_total);

// 字符串与基本的数据类型相加,把这个基本数据类型转成字符串后再生成新的字符串
var num_str = " " + a + b + c + true;
console.log(num_str);

简约表达式

  • +=, -=, *=, /=
1
2
3
4
5
// 简约的写法
a += c; // a = a + c;
a -= c; // a = a - c;
a *= c; // a = a * c;
a /= c; // a = a / c;
  • 自增++,自减 –
1
2
3
4
a ++;  // a = a + 1;
++ a; // a = a + 1;
a --; // a = a - 1;
-- a; // a = a - 1;

条件语句

if 语句

  • 单个条件

语法

1
2
3
4
if (条件判断)
{
主体
}

示例

1
2
3
4
> if (a > b)
{
console.log("a is bigger")
}
  • 默认否

语法

1
2
3
4
5
6
7
if (条件判断)
{
主体
} else
{
主体
}

示例

1
2
3
4
5
6
7
8
if (a > b)
{
console.log("a is bigger")
}
else
{
console.log("b is bigger")
}
  • 多个条件

语法

1
2
3
4
5
6
7
8
if(条件判断)
{
}
else if {
}
...
else {
}

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (a == 1)
{
console.log("a is 1")
}
else if(a == 2)
{
console.log("a is 2")
}
else if(a == 3)
{
console.log("a is 3")
}
else if(a == 4)
{
console.log("a is 4")
}
else
{
console.log("a is " + a)
}

switch 语句

语法

1
2
3
4
5
6
switch(变量) {
case 常量1:
break;
case 常量2:
break;
}

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var a = 2

switch (a)
{
case 1:
{
console.log("a is 1")
break
}

case 2:
{
console.log("a is 2")
break
}

case 3:
{
console.log("a is 3")
break
}
case 4:
{
console.log("a is 4")
break
}
default:
{
console.log("final a is " + a)
}
}

循环语句

while 语句

语法

1
while(表达式) { 循环体}

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
//while(条件成立) {循环体代码}; 
index = 0;
while(index < 10) { // 循环执行了10次;判断一下, 循环条件是否为真,为真就执行,否者就不执行循环体;
console.log(index);
index += 2;
}
console.log("out of while");
/*
// 死循环,如果任何时候,这个循环判断条件都为真,那么就是死循环,代码就在循环体里面一直执行;
while(true) { // 死循环,一直执行while语句
console.log("while call true");
}
*/

for 语句

语法

1
for(初始化; 循环条件; 循环迭代) {循环体}

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var walk = 0;
var i = 1;
for(walk = 0, i = 1; i <= 10; walk += 2, i ++) {
console.log(walk);
}

walk = 0;
i = 1;
for(; i <= 10; walk += 2, i ++) {
}
console.log("out for");

// 死循环,如果么有循环判断,默认为true
/*for(;;) {
console.log("loop for");
}*/

// 省略初始化与循环迭代语句;
i = 6;
for(; i < 10;) {
console.log("for loop");
i ++;
}

do while 语句

语法

1
do {}while(表达式);

示例

1
2
3
4
5
6
// for, while是先判断后执行,do{} while(); 先执行, 再判断是否继续执行循环
i = 0;
do {
i ++;
console.log(i);
}while(i < 10);

循环控制语句

continue

终止当前循环体的本次循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
i = 1;
for(i = 1; i <= 10; i ++) {
if (i == 8) {
continue; // 终止本次循环,但是仍然会执行 循环迭代语句 (i ++);
}

console.log("give hime " + i);
}

i = 1;
while(i <= 10) {
if (i == 8) {
console.log("while continue");
i ++;
continue; // 终止本次循环,回到循环判断,一定要注意循环判断变量,在continue,不要有死循环
}

console.log("while give hime " + i);
i ++;
}

break

结束最近一个循环体所有的循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
i = 1;
for(i = 1; i <= 10; i ++) {
if (i == 8) {
break; // 终止所有循环,跳出循环代码,但是不会执行 循环迭代语句 (i ++);
}

console.log("give hime " + i);
}
console.log("end_for", i); // i = 8;

i = 1;
while(i <= 10) {
if (i == 8) {
console.log("while continue");
i ++;
break; // 终止所有循环,跳出循环代码
}
console.log("while give hime " + i);
i ++;
}
console.log("end while");

字符串常用操作

indexOf

要查找具有另一个字符串的字符串,请使用indexOf函数:

1
2
3
4
var i = "this is a test".indexOf("is");
console.log(i);
->
2

substr和splice

要从字符串中提取子字符串,请使用substrsplice函数。

substr 获取要提取的字符串的起始索引和长度。splice取起始索引和结束索引:

1
2
3
4
5
6
7
8
9
10
11
var s = "this is a test string."

console.log(s.substr(19, 3))
console.log(s.substr(-5))
console.log(s.slice(19, 22))
console.log(s.slice(-5))
->
ng.
ring.
ng.
ring.

Split

要将字符串拆分为子字符串,请使用split函数并获取数组作为结果:

1
2
3
4
var s = "a|b|c|d|e|f|g|h".split("|");
console.log(s);
->
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

trim

trim函数从字符串的开头和结尾删除空格:

1
2
3
4
var s = "       cat   \n\n\n    ". trim();
console.log(s);
->
cat

replace

replace替换字符串

1
2
3
4
var s = "cat"
console.log(s.replace('cat', 'dog'))
->
dog
String 对象属性
属性 描述
constructor 对创建该对象的函数的引用
length 字符串的长度
prototype 允许您向对象添加属性和方法
String 对象方法
方法 描述
charAt() 返回在指定位置的字符。
charCodeAt() 返回在指定的位置的字符的 Unicode 编码。
concat() 连接两个或更多字符串,并返回新的字符串。
fromCharCode() 将 Unicode 编码转为字符。
indexOf() 返回某个指定的字符串值在字符串中首次出现的位置。
lastIndexOf() 从后向前搜索字符串。
match() 查找找到一个或多个正则表达式的匹配。
replace() 在字符串中查找匹配的子串, 并替换与正则表达式匹配的子串。
search() 查找与正则表达式相匹配的值。
slice() 提取字符串的片断,并在新的字符串中返回被提取的部分。
split() 把字符串分割为字符串数组。
substr() 从起始索引号提取字符串中指定数目的字符。
substring() 提取字符串中两个指定的索引号之间的字符。
toLowerCase() 把字符串转换为小写。
toUpperCase() 把字符串转换为大写。
trim() 去除字符串两边的空白
valueOf() 返回某个字符串对象的原始值。
String HTML 包装方法

HTML 包装方法返回加入了适当HTML标签的字符串。

方法 描述
anchor() 创建 HTML 锚。
big() 用大号字体显示字符串。
blink() 显示闪动字符串。
bold() 使用粗体显示字符串。
fixed() 以打字机文本显示字符串。
fontcolor() 使用指定的颜色来显示字符串。
fontsize() 使用指定的尺寸来显示字符串。
italics() 使用斜体显示字符串。
link() 将字符串显示为链接。
small() 使用小字号来显示字符串。
strike() 用于显示加删除线的字符串。
sub() 把字符串显示为下标。
sup() 把字符串显示为上标。

Math 常用功能

完整介绍:https://www.w3cschool.cn/jsref/jsref-obj-math.html

Math 属性
属性 描述
E 返回算术常量 e,即自然对数的底数(约等于2.718)。
LN2 返回 2 的自然对数(约等于0.693)。
LN10 返回 10 的自然对数(约等于2.302)。
LOG2E 返回以 2 为底的 e 的对数(约等于 1.414)。
LOG10E 返回以 10 为底的 e 的对数(约等于0.434)。
PI 返回圆周率(约等于3.14159)。
SQRT1_2 返回返回 2 的平方根的倒数(约等于 0.707)。
SQRT2 返回 2 的平方根(约等于 1.414)。
Math 方法
方法 描述
abs(x) 返回 x 的绝对值。
acos(x) 返回 x 的反余弦值。
asin(x) 返回 x 的反正弦值。
atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
atan2(y,x) 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。
ceil(x) 对数进行上舍入。
cos(x) 返回数的余弦。
exp(x) 返回 Ex 的指数。
floor(x) 对 x 进行下舍入。
log(x) 返回数的自然对数(底为e)。
max(x,y,z,…,n) 返回 x,y,z,…,n 中的最高值。
min(x,y,z,…,n) 返回 x,y,z,…,n中的最低值。
pow(x,y) 返回 x 的 y 次幂。
random() 返回 0 ~ 1 之间的随机数。
round(x) 把数四舍五入为最接近的整数。
sin(x) 返回数的正弦。
sqrt(x) 返回数的平方根。
tan(x) 返回角的正切。

垃圾回收

  1. 一个对象如果没有任何引用变量指向这个对象会被判定为垃圾对象;
  2. 在特定的时期js解释引擎会回收复杂对象的内存;
  3. 垃圾回收
    • 没有任何变量,保存了这个对象的引用;
    • 在特定的时候,由js解释引擎(v8)特定的时期回收

参考资料



文章链接:
https://www.zywvvd.com/notes/coding/node-js/node-js-grammer/node-js-grammer/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

node.js -3- 基本语法
https://www.zywvvd.com/notes/coding/node-js/node-js-grammer/node-js-grammer/
作者
Yiwei Zhang
发布于
2021年8月7日
许可协议