zoukankan      html  css  js  c++  java
  • (原创)JavaScript高级程序设计--学习笔记003-Boolean 类型

    Boolean 类型

        该类型只有两个字面值: true 和 false 。

        各种数据类型及其对应的转换规则:

            数据类型                      转换为true的值                              转换为true的值

            Boolean                       true                                                false

            String                          任何非空字符串                              "" (空字符串)

            Number                      任何非零数字值(包括无穷大)      0和 NaN (参见本章后面有关NaN的内容)

            Object                        任何对象                                         null

            Undefined                  n/a①                                              undefined

        需要注意的是,undefined是转换为false,0也是转换为false,所以,在if进行数值判断时,需要额外小心,例如:

            function test( num ) {

                if ( !num ) return;

                ...

            }

           test();  // !num 为true,return;

           test( 0 );  // 虽然传递了数值0,但是 !num 为true,return;

            if ( !num ) 这个判断本身的目的可能是判断是否有传递数值,但是当传递了数值0时,也会被认为没有传递数值;

  • 相关阅读:
    leetcode 18 4Sum
    leetcode 71 Simplify Path
    leetcode 10 Regular Expression Matching
    leetcode 30 Substring with Concatenation of All Words
    leetcode 355 Design Twitte
    leetcode LRU Cache
    leetcode 3Sum
    leetcode Letter Combinations of a Phone Number
    leetcode Remove Nth Node From End of List
    leetcode Valid Parentheses
  • 原文地址:https://www.cnblogs.com/wodehao0808/p/11770145.html
Copyright © 2011-2022 走看看