数据类型

270 阅读2分钟

1.比较

javascript字符串在进行大于(小于)比较时,会根据第一个不同的字符的ascii值码进行比较,当数字(number)与字符串(string)进行比较大小时,会强制的将数字(number)转换成字符串(string)然后再进行比较

console.log('13'>'3'); // 输出:false

console.log(5>'6'); // 输出: false

console.log('d'>'ABDC') // 输出: true

console.log(19>'ssf') // 输出 false

console.log('A'>'abcdef') // 输出 false

2.运算符 undefined, null, “” ,0 ,NAN ,false 转为布尔值都为false

&& || !

&& 看到假的就返回

两个表达式 ==> 先看第一个表达式转换为布尔值的结果,如果结果为真,那么他会看第二个表达式转换为布尔值的结果,然后如果只有两个表达式的话,只看到第二个表达式,就可以返回该表达式的值var a = 1 && 2 ==>2;

如果第一个为假。则返回第一个的值var a = 0 && 2 ==>0;

三个表达式 ==>原理也是一样。先看第一个

短路语句 2>1 && document.write(‘test’);==>test

|| 跟&& 相反,看到真的就返回

a = 0 && false && 2 ==>2

! 将值转为布尔值然后取反

3.条件语句

if...while

while(true){}

switch case

switch(){

  case “”:

  break;

}

continue;

不往下执行(终止本次的循环) ,但是继续循环

初识引用值

  • 数组
  • 对象

编程形式的区别

  • 面向过程(按照步骤,顺序来)
  • 面向对象(谁能干啥,谁能干啥)

类型转换 

  • typeof 
  • number 
  • string 
  • boolean 
  • object 
  • undefined 
  • function
  1. ypeof(“123”); 返回的都是string类型的
  2. typeof(null);==>object
  3. 显示类型转换方法:
  4. Number(); //a,undefined ==>NaN null == >0
  5. parseInt();
  6. //转为整形的数 只能是数子 true undefined不是数字的都转为NaN
  7. parseInt(num,numIndex);//numIndex 进制 将别的进制转为十parseInt(2,8);八进制的2转为十进制 2
  8. parseInt(10,2)==>2
  9. parseInt(100,2)==>4
  10. parseInt(demo,16);
  11. parseFloat()
  12. toString(radix);//null undefind 没有toString()
  13. var num = 10;
  14. num.toString(8); 将十进制转八进制
  15. String();
  16. Boolean()

隐式类型转换

  1. isNaN();
  2. var abc = “123”;
  3. isNaN(abc) ==> Number(abc) 是不是NaN;
  4. ++a / +a / -a
  5. ++ : + : - 转Number
  6. && ||
  7. true ==>1
  8. false ==>0
  9. false>true ==>false
  10. 2>1>3==>false
  11. undefined == null ==>true
  12. NaN == NaN ==> false
  13. 不发生类型转换 === 和 !==
  14. alert(a) 报错 为定义
  15. console.log(typeof(a)) ==> undefind 是字符串类型的