JS基础_其他进制的数字(了解)

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6         <script type="text/javascript">
 7             
 8             
 9             /*
10              * 在js中,如果需要表示16进制的数字,则需要以0x开头
11              *            如果需要表示8进制的数字,则需要以0开头
12              *                   如果要要表示2进制的数字,则需要以0b开头,但是不是所有的浏览器都支持
13              *     
14              */
15             
16             //十六进制
17             var a = 0x10;
18             console.log(a);//16
19             
20             a = 0xff;
21             console.log(a);//255
22             
23             a = 0xCafe;
24             console.log(a);//51966
25             
26             //八进制数字
27             a = 070;
28             console.log(a);//56
29             
30             //二进制数字,则需要以0b开头
31             //a = 0b10;
32             //console.log(a);//报错,这个内置浏览器不支持 Uncaught SyntaxError: Unexpected token ILLEGAL,火狐 谷歌是支持的
33             
34             //-----------------------------------------------------------------------------------------------------------
35             
36             //向"070"这种字符串,有些浏览器会当成8进制解析,有些会当成10进制解析
37             a = "070";
38             //可以在parseInt()中传递一个第二个参数,来指定数字的进制
39             a = parseInt(a,10);
40             console.log(typeof a);//number
41             console.log(a);//70
42             
43         </script>
44     </head>
45     <body>
46     </body>
47 </html>
原文地址:https://www.cnblogs.com/ZHOUVIP/p/7225310.html