1. 循环

1.1 语法格式

  1. for(i = 0; i < 10; i++){
  2. console.log(i);//执行语句
  3. }
  4. /****/
  5. var i = 0;
  6. while(i < 10){
  7. console.log(i);
  8. i++;
  9. }

1.2 基础算法

  1. /*1.不使用break或continue语句,当i>10时退出for循环*/
  2. var i = 1;
  3. for(;i;){
  4. console.log(i)
  5. i++;
  6. if(i>10){
  7. i=0;
  8. }
  9. }
  10. /*2.从0开始相加,总和大于100时结束*/
  11. var sum = 0;
  12. for(var i = 0; i < 100; i++){
  13. sum += i;
  14. if(sum > 100){
  15. break;
  16. }
  17. console.log(sum,i);
  18. }
  19. /*3.100以内的数跳过可以被7整除或个位数为7的数*/
  20. for(var i = 0; i <= 100; i++){
  21. if (i % 7 == 0 || i % 10 == 7){
  22. continue;
  23. }
  24. console.log(i);
  25. }
  26. /*4.打印0-100的数,
  27. 要求:
  28. ()只能有一句,不能写比较
  29. {}不能出现i++,i--
  30. */
  31. var i = 101;
  32. for(;i--;){
  33. console.log(i);
  34. }
  35. /*5.10的n次方*/
  36. var n = 5;
  37. var sum = 1;
  38. for(var i = 0; i < n;i ++){
  39. sum *= 10;
  40. }
  41. console.log(sum);
  42. /*6.n的阶乘*/
  43. var n = 5;
  44. var num = 1;
  45. for(var i = 1; i <= n; i++){
  46. num*=i;
  47. }
  48. console.log(num);
  49. /*7.打印三个数中最大的数*/
  50. var a = 1,
  51. b = 2,
  52. c = 3;
  53. console.log(
  54. (a > b ? a : b) > c ? (a > b ? a : b) : c
  55. );
  56. /*8.打印100以内的质数*/
  57. var count = 0;
  58. for(var i = 2; i <= 100; i++){
  59. for(var j = 1; j <= i; j++){
  60. if(i % j == 0){
  61. count++;
  62. }
  63. }
  64. if(count == 2){
  65. console.log(i);
  66. }
  67. count = 0;
  68. }

2.引用值

2.1 种类

  1. array
  2. object
  3. function
  4. date
  5. RegExp

    2.2 数组

  6. 语法格式

    1. var arr = [1, 2, 3, 4, 5, 6, 7];
    1. 索引从o开始
    2. 常用函数
      1. arr.length //取数组长度

      2.3 对象

  7. 语法格式

    1. var person = {
    2. //属性名/键名:属性值/键值
    3. name: '小夏'
    4. age: 18.
    5. job: 'WEB开发工程师'
    6. }

    3.typeof()方法

    3.1 使用方法

  8. typeof()能测出的类型

    1. number
    2. string
    3. boolean
    4. object
      1. 具体的某一对象 -> object
      2. 引用类型 -> Object
  9. 具体测值:

    1. typeof(123); //number
    2. typeof('123'); //string
    3. typeof(true); //boolean
    4. typeof({}); //object
    5. typeof([]); //object
    6. typeof(null); //object
    7. /*
    8. 1.typeof(null) 返回 object 是一个历史遗留的bug,Js中的基本数据类型都存储在32位的二进制单元当中,低三位的数字用于表示该数据的类型。低三位数字和表示类型的对应关系如下:
    9. 000: object - 当前存储的数据指向一个对象。
    10. 1: int - 当前存储的数据是一个 31 位的有符号整数。
    11. 010:double - 当前存储的数据指向一个双精度的浮点数。
    12. 100: string - 当前存储的数据指向一个字符串。
    13. 110: boolean - 当前存储的数据是布尔值。
    14. typeof的实现就是通过判断低三位的数字来判断值类型的。当传入一个null的时候,因为null存储时32位的数字表示都是0,所以低三位也是0,typeof就将其认定为object类型的了
    15. */
    16. typeof(undefined); //undefined
    17. typeof(function(){}); //function
    18. /**/
    19. typeof(1 - 1); //number
    20. typeof(1 - "1"); //number
    21. /*未定义a*/
    22. typeof(a); //undefined
    23. typeof(typeof(a)); //string

    4. 显式、隐式类型转换

    4.1 显示

  10. Number()

    1. var a = '123';
    2. typeof(Number(a) + ' - ' + Number(a)); // number - 123
    3. a = true;
    4. typeof(Number(a) + ' - ' + Number(a)); // number - 1
    5. a = null;
    6. typeof(Number(a) + ' - ' + Number(a)); // number - 0
    7. a = undefined;
    8. typeof(Number(a) + ' - ' + Number(a)); // number - NaN
    9. a = 'a';
    10. typeof(Number(a) + ' - ' + Number(a)); // number - NaN
    11. a = '1a';
    12. typeof(Number(a) + ' - ' + Number(a)); // number - NaN
    13. a = 3.14;
    14. typeof(Number(a) + ' - ' + Number(a)); // number - 3.13
  11. parseInt

    1. var a = '123';
    2. typeof(parseInt(a)) + ' - ' = parseInt(a);//number - 123
    3. a = ture;
    4. typeof(parseInt(a)) + ' - ' = parseInt(a);//number - NaN
    5. a = null;
    6. typeof(parseInt(a)) + ' - ' = parseInt(a);//number - NaN
    7. a = undefined;
    8. typeof(parseInt(a)) + ' - ' = parseInt(a);//number - NaN
    9. a = NaN;
    10. typeof(parseInt(a)) + ' - ' = parseInt(a);//number - NaN
    11. a = '3.14';
    12. typeof(parseInt(a)) + ' - ' = parseInt(a);//number - 3
    13. a = '3.99';//不是四舍五入
    14. typeof(parseInt(a)) + ' - ' = parseInt(a);//number - 3
    15. a = 'abc123';
    16. typeof(parseInt(a)) + ' - ' = parseInt(a);//number - NaN
    17. a = '123abc';
    18. typeof(parseInt(a)) + ' - ' = parseInt(a);//number - 123
    19. /*双参数*/
    20. parseInt(a, 16); //代表以16进制为基础转换a,参数可取2-36
  12. parseFloat

    1. var a = '3';
    2. parseFloat(a);//3
    3. a = '3.141592654'
    4. parseFloat(a.toFixed(2));//保留2位小数,且四舍五入
  13. String()


      1. var a = 123;
      2. typeof(String(a) + ' - ' + String(a);//string - '123'
    1. String() 和 toString()

      1. toString()可以将所有的的数据都转换为字符串,但是要排除null 和 undefined
      2. toString() 括号中的可以写一个数字,代表进制,对应进制字符串
        二进制:.toString(2);
        八进制:.toString(8);
        十进制:.toString(10);
        十六进制:.toString(16);
      3. String()可以将null和undefined转换为字符串,但是没法转进制字符串
  14. Boolean()

    1. Boolean(1);//true
    2. Boolean(null);//false
    3. /*undefined,null,NaN,"",0,false 都是false,其他为true*/

    4.2 隐式

  15. 案例 ```javascript var a = ‘123’; a++; console.log(a); //1234

a = ‘a’ + 1;//String(1); console.log(a);//a1

a = ‘3’ * 2;// & / - % 则 str -> number console.log(a);//6

a = ‘1’ > 2; console.log(a);//false

a = 1 > ‘2’;//Number() console.log(a);//flase

a = ‘a’ > ‘b’;//ascii console.log(a);//false

a = 1 == ‘1’; console.log(a);//true

a = 1 === ‘1’;//不进行隐式转换 console.log(a);//false

a = NaN == NaN; console.log(a);//flase

a = undefined > 0;//=’ ‘<’ -> false console.log(a);//false

a = null > 0; //‘=’ ‘<’ -> false console.log(a);//false

a = undefined == null; console.log(a);//true / undefined 值是派生自 null 值的,ECMAScript 标准规定对二者进行相等性测试要返回 true,可以理解为 null 和 undefined 都代表着无效的值,所以二者相等,但由于是两种不同的原始数据类型,所以不全等。 /

var a1 = 2 > 1 > 3; var a2 = 2 > 1 == 1; console.log(a1,a2);//false true

  1. 2. isNaN
  2. ```javascript
  3. /*
  4. isNaN -> Number(值)
  5. */
  6. isNaN(NaN);//true
  7. isNaN(123);//false
  8. isNaN('123');//false 经过隐式转换
  9. isNaN('a');//true
  10. isNaN(null);//false
  11. isNaN(undefined);//ture