初入JS(二)

判断类型

  typeof以及返回类型(都是字符串)

扩展

    如何判断类型?
    1.typeof
    2.instanceof
    3.Object.prototype.toString.call()

    NaN的类型是number

注意

  如下定义并赋值a,那么typeof一下a,返回的是什么类型?

1
2
var a = function(){};
console.log( typeof a);

结果
  事实证明返回的是function

  整理一下各个类型的typeof

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // 尽管NaN是"Not-A-Number"的缩写
typeof Number(1) === 'number'; // 但不要使用这种形式!

// Strings
typeof "" === 'string';
typeof "bla" === 'string';
typeof (typeof 1) === 'string'; // typeof总是返回一个字符串
typeof String("abc") === 'string'; // 但不要使用这种形式!

// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(true) === 'boolean'; // 但不要使用这种形式!

// Symbols
typeof Symbol() === 'symbol';
typeof Symbol('foo') === 'symbol';
typeof Symbol.iterator === 'symbol';

// Undefined
typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined';

// Objects
typeof {a:1} === 'object';

// 使用Array.isArray 或者 Object.prototype.toString.call
// 区分数组,普通对象
typeof [1, 2, 4] === 'object';

typeof new Date() === 'object';

// 下面的容易令人迷惑,不要使用!
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String("abc") === 'object';

// 函数
typeof function(){} === 'function';
typeof class C{} === 'function'
typeof Math.sin === 'function';
typeof new Function() === 'function';

转换类型

1.显式类型转换

  调用转换方法,主动转换
  a.Number()
  b.String()
  c.Boolean()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//显式类型转换
var a = '123';
console.log( typeof Number(a) );

var a = 'hello';
console.log( typeof Number(a) ); //NaN

var a = true;
console.log( Number(a) ); // true -> 1 , false -> 0

var a = 123;
console.log( typeof String(a) ); // '123'

var a = true;
console.log( String(a) ); // 'true'

var a = '';
console.log( Boolean(a) ); //只要字符串有内容就转成true,空字符串转成false

var a = 0;
console.log( Boolean(a) ); //只要数字不为0,就转成true,0会转成false

2.隐式类型转换

  被动转换,由JS程序自动进行转换。
  + - * / == 隐式类型转换
  + 数值 - 负数值

1
2
3
4
5
6
//隐式类型转换
var a = '1';
var b = 2;

console.log( a + b ); //隐式类型转换 b -> 2 -> '2' ,然后在连接a变量
console.log( a - b ); //隐式类型转换 a -> '1' -> 1 , 然后在相加b变量

比较类型

挑一些重点来说了,就不把知识点一一罗列了,具体可以查看w3cSchool

== 和 = 之间的区别

   ==进行比较,而=进行赋值操作

比较时候的几种情况

1.当两边都是字符串的情况下
  按位进行比较,字符的unicode编码进行比较

1
2
3
4
//两边都是字符串的情况 : 按位进行比较,字符的unicode编码进行比较
console.log( 'a' >= 'b' );
console.log( 'a'.charCodeAt() ); //查看字符对应的unicode编码
console.log( '5' >= '12' ); // true

2.当两边不都是字符串的情况下
  直接看如下代码吧

1
2
3
4
//两边不都是字符串的情况 : 把其他类型转成数字类型后,再比较
console.log( 5 >= '12' ); // 5 >= 12 // false
console.log( true == 1 ); // true -> 1 -> 1==1 // true
console.log( true == 2 ); // false

查看字符对应的unicode编码方法:charCodeAt()
3.特殊类型的比较
  null、undefined、NaN的比较
  老规矩直接上代码,清晰明了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//特殊类型的比较 
nullundefined比较的时候,返回true。除了下面三种情况,跟其他任何值比较都是false
console.log( null == null );
console.log( undefined == undefined );
console.log( null == undefined );
console.log( null == false );
console.log( null == true );
console.log( undefined == false );
console.log( undefined == true );

console.log( NaN == NaN ); //false
//ES6 : Object.is() : 修正一些判断的
console.log( Object.is(NaN , NaN) );
console.log( +0 == -0 );
console.log( Object.is(+0 , -0) );

其中 Object.is()是ES6中为了修正历史遗留问题而推出的。


JS运算符(五大类)

因为运算符基本上不需要语言赘述,故直接扔上图和代码

1.算术运算符(10种)

算术运算符

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
//算术运算符(10种)
// + - / * %
// ++ --
// + -
// **

//4%7 -> 4 当被除数小于除数的时候,余数就是被除数自身

//123秒
//123%60 , 转换后剩余的秒数

var a = 1;
a++;
++a;

console.log( a++ ); // 先返回a,再对a+1操作
console.log( ++a ); // 先对a+1操作,然后再返回a

var a = 1;
a++;
++a;
console.log(a)
//+ 数值 -负数值

var a = '123';
console.log( typeof +a );

//指数运算符 , 从右向左计算 (ES6)
console.log( 2 ** 2 ** 3 );
console.log( 2 * 2 * 2 );
console.log( Math.pow(2,5) );

2.赋值运算符(简单、复合)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//赋值运算符(简单与复合)

var a = 1;
a = a + 2; //简单的赋值运算符
console.log(a);

var a = 1;
a += 2; //复合的赋值运算符
console.log(a);

var a = 1;
var b = 2;
a = b + 3;

a++;
a += 1;

var a = 1;

a = (a/=2) + a;

console.log(a);

//(1 + 2) * 3

3.比较运算符(8种)

比较运算符

1
2
3
4
5
6
7
8
9
10
11
//比较运算符(8种)

//== === != !==

//===表示严格相等,值和类型都要相等,才相等

console.log( 2 === '2' );

console.log( 2 != 3 );

console.log( 2 !== '2' );

4.逻辑运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//逻辑运算符 : &&(与) ||(或) !(非)

//&& : 逻辑两边都为真,结果才为真,逻辑两边有一边不为真,结果就为假。

console.log( 1 < 2 && 5 > 4 );

//|| : 逻辑两边有一边为真,结果就为真,逻辑两边都为假,结果才为假。

console.log( 1 > 2 || 5 < 4 );

//! : 逻辑的取反,真的就是变成假的,假的就会变成真的
console.log( !(1 > 2) );

var 艺术生 = '小明';
var 高考成绩 = 300;
var 艺考成绩 = 200;
console.log( 艺术生 , 高考成绩>200 && 艺考成绩>100 );

var number = 70;
console.log( 3 < number < 10 ); //✖
console.log( 3 < number && number < 10 ); //✔

5.条件运算符

三目运算符:条件?输出1:输出2

1
2
3
4
5
6
//三元运算,三目运算  ? :
//当条件为真,就返回问号后面的内容,当条件为假,就返回冒号后面的内容
//执行顺序从右向左
console.log( false ? 1 : 2 );
console.log( false ? 1 : 2 ? 3 : 4 );



初入JS(二)
https://moewang0321.github.io/2019/08/05/2019-08-06-初入JS(二)/
作者
Moe Wang
发布于
2019年8月5日
许可协议