Mistakes that can be encountered about data types when working with javascript

Tram Ho

1. The data type of NaN

NaN or not a number. It sounds like this is not a number, so of course its data type will not be number? However, when opening the browser and executing console.log (type of NaN), the output is number.

In fact, NaN is a special value in the type of Number along with (Infinity, -Infinity) too. NaN will be returned when performing a calculation that results in, for example, an unspecified numeric value

  • Divide 0 by 0 (Divide another number by 0 to divide into Infinity)
  • Divide Infinity by Infinity
  • There is one operand in the calculation as NaN
  • Convert from string to number a non-numeric value.

In addition NaN also unordered ie NaN can not be smaller, equal or larger than anything.

2. Output of 99999999999999999999 (20 digits)

How much do you expect the output of the number to be?

The reason for this is because Javascript only has a Number data type that represents numbers. And it is implemented as IEEE 754.’s double precision floating-point number, which means that there will be no actual Integer integer data type in Javascript. This is not too big a problem in TH you want to work with only large integers in Javascript you can use the BigInt data type.

3. 0.1 + 0.2 = 0.3?

This error does not only occur with Javascript alone

You remember about infinite and finite decimal numbers? In decimal (the base 10 system that humans are best exposed to) it can only express fractions using a prime factor of 10 (The prime factors of 10 are 2 and 5). Therefore, 1/2 (0.5), 1/4 (0.25), 1/5, 1/8 and 1/10 can all be clearly expressed because all denominators use prime factors. is 10. In contrast, 1/3 (0.33333333 ….), 1/6 (0.1666666 …) and 1/7 are all infinite decimal numbers because their denominators use a prime factor of 3 or 7. You might wonder if 0.1 and 0.2 can clearly denote finite decimal numbers? But the problem is that computers use base 2 instead of base 10 like humans, where the only prime factor of 2 is itself, so the fractions 1/2, 1/4, 1 / 8 will be clearly displayed in the calculator, but the numbers 1/5 (0.1) and 1/10 (0.2) are clearly expressed in base 10 but the computer works in base 2 expression cannot be accurate.

4. Math.max () and Math.min ()

The Math Library is a library that is used quite a lot in javascript. However, there is a small note for these two functions that in TH you do not pass parameters to it, it will return the default values ​​as follows:

5 Javascript usually automatically converts the data type in expressions

JavaScript is a loosely typed language and most expressions automatically implicitly convert a value into the right type so it can be done. However, there are also cases where the outcome of an expression is undesirable. So pay attention to Implicit Conversion in Javascript

Implicit conversions are common in Javascript when that expression is converted to string (toString) or number (toNumber). Here are some cases you might come across

  • [] + []
    Because the array is not a primitive data type, the toString function will be called and the resulting array will be an empty character. Therefore the result of adding 2 empty arrays is the addition of 2 empty string characters ==> empty character

  • [] + {}
    This is the same as above but many of the {} toString function will return ‘[object Object]’

  • “1” + 0 The addition here is also concatenated. Therefore, Javascript prioritizes handling strings and converting zeros to String strings

  • “10” – “0” Subtraction has no meaning in strings so Javascript converts both “10” and “0” into numeric characters

Refer:

Share the news now

Source : Viblo