2024-11-15 11:16

JavaScript的??=运算符

码自答

WEB前端

(32)

(0)

收藏

JavaScript的??=运算符,表示当变量的值为null或者是undefined时,赋给变量一个默认值。

例如以下代码:

<script type="text/javascript">
            var school;
            
            school ??= "wanmait";     
            
            document.write(school);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        </script>

表示school的值为null或者undefined时,school的默认值为"wanmait",

输出school的值为"wanmait"

等价于

<script type="text/javascript">
            var school;
            
            if(school==null||school==undefined)
            {
                school = "wanmait";
            }
            
            document.write(school);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        </script>




0条评论

点击登录参与评论