This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

We’ve learned a few things about JavaScript:

  • -number – This is a function
  • Infix operators in JavaScript
  • Do you know what JavaScript Typeof is?

In this article, we will learn about automatic type conversion in JavaScript. There are some minor bugs that need to be noticed

JavaScript Automatic Type Conversion

Let’s start with the following simple code:

console.log('621' - 1) / / 620
console.log('621' + 1) / / 6211
console.log(false= =0) // true
Copy the code

The minor “bug” is the automatic type conversion, see MDN explanation:

Type conversion (or type conversion; Type conversion (typecasting) is the conversion of data from one Type to another. Implicit conversions occur when the compiler automatically assigns values, but there are also ways to force explicit conversions in code. For example, in the expression 5 + 2.0, the integer 5 is implicitly converted to float, but Number(“0x11”) and “0x11” are explicitly converted to the numbers 17.

When a string such as “this is a string” is converted to?? Or undefined, you get the value NaN. Further arithmetic operations on nans continue to produce Nans… So when you see unexpected results in these places, keep an eye out for automatic type conversions.

Null, and undefined

Null and undefined are special in JavaScript

When using == to compare values of the same type, the result is easy to predict: When two values are identical, you should get true, except for NaN. But when comparing different types, JavaScript uses a complex and confusing set of rules to determine what to do. In most cases, it tries to convert one value to the type of the other. However, if null or undefined is present on either side of the operator, true is generated only if either side is null or undefined.

console.log(null= =undefined) // ==> true
console.log(nul1 == 0) // ==> false
Copy the code

In real development, if you want to test whether a value has an actual value rather than null or undefined, you can use == (or! The ==) operator compares it to NULL.

However, it is often necessary to test whether something refers to the exact value false. When using ==, expressions such as 0 == false and “” == false are correct.

When we do not want any automatic type conversions to occur, we use two other operators: === and! = =. The former tests whether one value is exactly equal to another, and the latter tests whether they are not. . The comparison operator === is recommended to prevent automatic type conversions. But you can use the == operator if you are sure that both are of the same type

Come on

Learning, recording and accumulating is a long process! Come on! Buy Less by Know More!