1024programmer Java Some small knowledge points in javascript data types (recommended)

Some small knowledge points in javascript data types (recommended)

1. undefined and null

undefined is a variable rather than a keyword, so it can be reassigned. In order to avoid ambiguity, it is generally recommended to use void 0 to obtain undefined;

null is a keyword, so you can use null with confidence.

Undefined and null have certain meaning differences. null means: “defined but empty”. Therefore, in actual programming, we generally do not assign variables to undefined. This ensures that all variables with a value of undefined are in a natural state that has never been assigned a value.

 function test(){
   var undefined = 123;
   console.log(undefined)
 }
 test() ==> 123

2. +0 and -0

There are +0 and -0 in Javascript. There is no difference between them in addition operations. To distinguish them, you need to use 1/x 

 3/0 ==> Infinity
 3/-0 ==> -Infinity

3. 0.1 + 0.2 = 0.3?

In JS, 0.1+0.2 is not equal to 0.3, because if 0.1 is represented in binary on a computer, an infinite loop of digits will occur. Similar to the decimal system, one-third is an infinitely repeating decimal.

So when you perform mathematical operations on these recurring decimals and convert the binary data into decimal, it is still an infinite recurring decimal. After intercepting the end, the value obtained is 0.30000000000000004

The correct comparison method is to use the minimum precision value provided by JS and check whether the absolute value of the difference between the left and right sides of the equation is less than the minimum precision

 Math.abs(0.1 + 0.2 - 0.3) <= Number.EPSILON
 true

4. The difference between 666 and new Number(666)

666 is a number type number, and Number(666) is an object type data.

 'HELLO'.charAt(1) ==》 'E'

In the on-site code, the dot (.) operator provides boxing operations and constructs a temporary object based on a basic type, so the method of the corresponding object can be called on the basic type

5. parseInt and parseFloat

parseInt(string, radix)

 parseInt(15,6)
 11 ==> 1*6+5

 parseInt(0x15)
 21 ==> 16*1+5

parseFloat directly parses the original string as decimal, and it does not introduce any other bases

6. Boxing conversion

Each basic type Number, String, Boolean, and Symbol has a corresponding class in the object. The so-called boxing conversion is to convert the basic type into the corresponding object.

Symbol is special. You cannot directly use new Symbol to obtain the corresponding object. You can use Object(Symbol(‘hello’)) to obtain it.

  var symbolObject = Object(Symbol("a"));

   console.log(typeof symbolObject); //object
   console.log(symbolObject instanceof Symbol); //true
   console.log(symbolObject.cOnstructor== Symbol); //true

7. Unboxing conversion

When converting an object into a number or string, unboxing conversion will be performed by default, that is, the object’s built-in toString or valueOf method will be called to convert the object into a basic type

An obj object is defined here, and its valueOf and toString methods are rewritten for testing. It can be seen that when Number (obj), valueOf takes precedence over String, but when neither of these two methods returns the basic type data, an error will occur.

When the valueOf method returns basic type data, obj is successfully converted into a number, and the toSring method is not executed.

The toString method will only be called when valueOf returns a non-basic type. Only when unboxing conversion from Object to String will the toString method be called first.

Note: ES6 allows objects to override the original behavior by explicitly specifying toPrimitive Symbol.

 var o = {
     valueOf : () => {console.log("valueOf"); return {}},
     toString: () => {console.log("toString"); return {}}
   }

   o[Symbol.toPrimitive] = () => {console.log("toPrimitive"); return "hello"}


   console.log(o + "")
   // toPrimitive
   // hello

The above is the summary and integration of some small knowledge points in Javascript data types introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support of the website!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/714800

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索