[Javascript] Dealing with Number in Javascript

Write big number

// NOT
100000

// Better
100_000
1e5

 

Shorthands syntax for floating number

// Normal
0.123

// The same
.123

// eX also apply to floating number
3.14e10 // 31400000000
console.log(0.123e10 === .123e10) // true

 

8进制

Start with 0 ⚠️

 

When start with 0, there is a issue with 0809, there shouldn't exist.

Better way is using prefix 0o

 

Start with 0o

As you can see, it throws the error for invalid syntax.

 

16进制

0xabef3  // 704243

 

2进制

0b010101 // 21

 

Number toString

Due to a valid number is written like this

xxx.xxx

Therefore JS expects 123.follows by numbers but we give a toString(), then it throws error.

You show do:

123..toString()or save into a variable then call toString()

posted @ 2024-12-03 16:59  Zhentiw  阅读(3)  评论(0编辑  收藏  举报