JS基础知识——变量类型和计算(一)

  1. JS中使用typeof能得到的哪些类型?
  2. 何时使用===何时使用==?
  3. JS中有哪些内置函数?
  4. JS变量按照存储方式区分为哪些类型,描述其特点?
  5. 如何理解JSON?

知识点梳理

一、变量类型:

(1)值类型&引用类型

 1 //值类型
 2 var a = 100;
 3 var b = a;
 4 a=200;
 5 console.log(b)  //100
 6 
 7 //引用类型
 8 var a = {age:12}
 9 var b =a;
10 b.age = 20;
11 console.log(a.age)//20 

(2)typeof运算符详解【只能区分值类型,函数和对象】

1 typeof undefined  //"undefined"
2 typeof 'abc'  //"string"
3 typeof 123  //"number"
4 typeof true //"boolean"
5 typeof {}  //"object"
6 typeof []  //"object"
7 typeof null  //"object"
8 typeof console.log  //"function"

二、变量计算——强制类型转换

a.字符串拼接

1 var a = 100 +10     //110
2 var b = 100 + '10'  //'10010'

b.==运算符

1 100 == '100' //true
2 0 == ''   //true
3 null == undefined //true

c.if语句

 1 var a = true
 2 if(a){
 3     //...
 4 }
 5 var b =100
 6 if(b){
 7     //...      
 8 }
 9 var c = ' '
10 if(c){
11    //...
12 }

d.逻辑运算

1 console.log(10&&0)  // 0
2 console.log(' ' ||  'abc' )  // 'abc'
3 console.log(!window.abc)  //true
4 
5 //判断一个变量会被当做true OR false
6 var a =100
7 console.log(!!a)

1、JS中使用typeof能得到的哪些类型?【undefined string number boolean object function】

2、何时使用===何时使用==?

1 if(obj.a == null){
2     //这里相当于obj.a === null || obj.a ===undefined ,简写形式
3     //这是jQuery源码中推荐的写法  
4 }

3、JS中有哪些内置函数?

Object、Array、Boolean、Number、String、Function、Date、RegExp、Error

4、JS变量按照存储方式区分为哪些类型,描述其特点?

 1 //值类型
 2 var a = 100;
 3 var b = a;
 4 a=200;
 5 console.log(b)  //100
 6  
 7 //引用类型
 8 var a = {age:12}
 9 var b =a;
10 b.age = 20;
11 console.log(a.age)//20 

5、如何理解JSON?

1 //JSON只是一个JS对象
2 JSON.stringify({a:10,b:20})
3 JSON.parse('{"a":10,"b":20}')

  

原文地址:https://www.cnblogs.com/chorkiu/p/10615520.html