JavaScript总结3

1.三目运算符

        表达式 ? 符合表达式结果 : 不符合表达式结果

            a>=18 ? console.log('成年人') : console.log('未成年')

2.显式类型转换

    1.将其他类型转换为string类型

        1.使用字符串拼接 +''

            console.log(a+'',typeof(a+''));

        2.使用toString()函数

            1.使用toString方法 null undefined 不可以调用toString 没有包装

                console.log(a.toString(),typeof(a.toString()));

            2.将number转为string  调用toString()放入一些进制参数

                console.log(a.toString(2)); //将number转为2进制string

        3.使用String()包装器函数

            console.log(String(a), typeof String(a));

    2.其他数据类型转换为boolean

        1.使用Boolean()包装器函数 true false

            console.log(Boolean(10),typeof Boolean(10));

        2.使用 !! 取反  

            console.log(!!10,typeof (!!10));

    3.将其他数据类型转换为number类型

        console.log(![]==[]) //true`

        1.使用 +

            console.log(+true, +false, +'10', typeof (+true), typeof (+false), typeof (+'10'));

        2.使用Number()包装器函数

            console.log(Number(true),typeof Number(true));

        3.使用parseInt()方法 取整

            console.log(parseInt(true),typeof parseInt(true));

            console.log(parseInt("234.1")); //234;小数点后面的数值省略

        4.使用parseFloat()方法

            console.log(parseFloat(true),typeof parseFloat(true));

3.流程控制语句

    1.if 语句

        只有当指定条件为 true 时,该语句才会执行代码。

            if (condition){当条件为 true 时执行的代码}

    2.if...else 语句

        使用 if....else 语句在条件为 true 时执行代码,在条件为 false 时执行其他代码。

            if (condition){条件为 true 时执行的代码}else{当条件不为 true 时执行的代码}

    3.if...else if...else 语句

        使用 if....else if...else 语句来选择多个代码块之一来执行。

    4.switch 语句

        switch 语句用于基于不同的条件来执行不同的动作。

        使用 switch 语句来选择要执行的多个代码块之一。

        default关键词来规定匹配不存在时做的事情:

            switch(n)

            {

                case 1:

                    执行代码块 1

                    break;

                case 2:

                    执行代码块 2

                    break;

                default:

                    与 case 1 和 case 2 不同时执行的代码

            }

4.循环语句

    1.for循环

        for (开始条件; 结束条件;迭代条件){}

    2.for-in循环

        for..in用于遍历数组或者对象的属性

            for(var key in arr){console.log(key+"--"+arr[key]);}

    3.while 循环

        只要指定条件为 true,循环就可以一直执行代码块。

            while (*条件*){需要执行的代码*}

    4.do/while 循环

        该循环会在检查条件是否为真之前执行一次代码块,然后如果条件为真的话,就会重复这个循环。

            do{需要执行的代码}while (条件);

5.递归

    函数自己调用自己 停不下来

    需要有 跳出的条件

        function fc(n) {

            // 跳出条件

            if(n===1){return 1}

            return fc(n-1) * n

        }

        console.log(fc(3));

        // fc(1) 1

        // fc(2) fc(1) * 2

        // fc(3) fc(2) * 3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值