Is there “0b” or something similar to represent a binary number in Javascript

后端 未结 10 1260
太阳男子
太阳男子 2020-11-29 02:32

I know that 0x is a prefix for hexadecimal numbers in Javascript. For example, 0xFF stands for the number 255.

Is there something similar f

10条回答
  •  青春惊慌失措
    2020-11-29 03:06

    No, but you can use parseInt and optionally omit the quotes.

    parseInt(110, 2); // this is 6 
    parseInt("110", 2); // this is also 6
    

    The only disadvantage of omitting the quotes is that, for very large numbers, you will overflow faster:

    parseInt(10000000000000000000000, 2); // this gives 1
    parseInt("10000000000000000000000", 2); // this gives 4194304
    

提交回复
热议问题