Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Javascript基础】常见类型判断与转换技巧 #3

Open
Sukiey opened this issue Jun 27, 2020 · 0 comments
Open

【Javascript基础】常见类型判断与转换技巧 #3

Sukiey opened this issue Jun 27, 2020 · 0 comments

Comments

@Sukiey
Copy link
Owner

Sukiey commented Jun 27, 2020

类型比较

比较与相等

  • 等于操作符 ==

    在使用等于操作符时,会发生强制类型转换。

  • 严格的等于操作符 ===

typeof操作符

typeof null === 'object'
typeof undefined === 'undefined'

instanceof操作符

类型转换

JavaScript 深入之头疼的类型转换(上)

JavaScript 深入之头疼的类型转换(下)

  • 转字符串

    '' + 10 === '10'
  • 转数值

    +'10' === 10; // true
    
    Number('010') === 10
    parseInt('010', 10) === 10  // 用来转换为整数
    
    +'010.2' === 10.2
    Number('010.2') === 10.2
    parseInt('010.2', 10) === 10

    使用一元的加号操作符,可以把字符串转换为数字。

  • 转布尔型

    !!'foo';   // true
    !!'';      // false
    !!'0';     // true
    !!'1';     // true
    !!'-1'     // true
    !!{};      // true
    !!true;    // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant