Any value used in these contexts that is not already Boolean is implicitly forced into a Boolean value, using the rules of the abstract ToBoolean operation discussed earlier in this chapter. You`re probably still wondering what it`s about~, or why it`s really important for a forced discussion. Let`s get straight to the point quickly. In this chapter, we looked at how JavaScript-like conversions occur, which are called compulsions and can be characterized as explicit or implicit. A type system is a set of logical rules that assign a type (or type of data) to different constructs of a computer program. You can read a lot about the type system on other websites, but suffice it to say that the JavaScript-like system is the reason it knows that “1” is a string and 1 is a number: Implicit type constraint: when the logical context is displayed or logical operations are performed by logical operators, The type constraint to the Boolean data type is implicit. As we mentioned in Chapter 1, the debate about whether coercion is a useful feature or a flaw in language design (or somewhere in between!) has been raging since day one. If you`ve read other popular books on JS, you know that the dominant message is that coercion is magical, bad, confusing, and just a bad idea. But the ES6 symbols introduce a gotcha into the coercive system, which we must mention briefly. For reasons that go far beyond the scope of what we will discuss in this book, the explicit compulsion of a symbol in a chain is allowed, but the implicit constraint of it is not allowed and generates an error. Implicit type constraint: When it comes to other data types with numbers using comparison operators, arithmetic operators, binary operators, and non-strict equality operators(==), the other data types are implicitly converted to numbers. On the surface, this phrase sounds like a form of explicit Toboolean coercion, because it is obvious that only true or false emerge from the operation. Implicit type constraint: When a string data type value is concatenated (using the + arithmetic operator) with another data type, that “other” data type is implicitly converted to a string.
Examples: The non-strict equality or equality operator also implicitly leads to a type constraint. But why? because the equality operator (==) converts the other operands of data type to operands of common or equal data type, and then compares the equality of the two operands. Let`s look at some examples – overall, there are relatively few cases where implicit coercion is really dangerous. But in these places, for safety reasons, definitely use ===. Type constraint in JavaScript is a process in which a value of one type is implicitly converted to another type. This is done automatically by JavaScript so that your code does not encounter. But as you`ll see in this article, type constraint can actually be the cause of errors. So if you pass a value like Infinity (the result of 1/0 obviously), what kind of string representation would make the most sense for their compulsion? Only two sensible choices come to mind: “infinity” and “∞.” JS chose “Infinity”. I`m glad that`s the case. (Russian) Современный учебник Javascript — learn.javascript.ru/. Especially these two sides on type constraint.
Unfortunately, there is no “strict relational comparison” as with equality. In other words, there is no way to prevent implicit constraint in relational comparisons such as a < b, except to ensure that a and b are explicitly of the same type before the comparison is made. Here are some other JavaScript limitations I found. JavaScript can calculate between Boolean and numeric types because Boolean true and false implicitly have numeric values 1 and 0 Implicit type constraint: The synonym for the word "implicit" is "indirect". As the word implies, implicit type coercion in JavaScript refers to converting values from one data type to another data type without the indirect intervention of the developer or type constraint. Simply put, we can say that the implicit type constraint is performed automatically by Javascript itself. The most important piece of advice I can give you: Review your program and justify the values that can be displayed on both sides of a comparison ==. To effectively avoid problems with such comparisons, here are some heuristic rules to follow: I think one case where implicit coercion can really shine is simplifying certain types of complicated Boolean logic into simple numerical addition.
Of course, this is not a general-purpose technique, but a specific solution for certain cases. The unary – operator also applies as +, but it also reverses the sign of the number. However, you cannot put two (–) side by side to reverse the character because it is parsed as a decrement operator. Instead, you would have to do –“3.14” with a space in between, and that would lead to the constraint at 3.14. However, seven of the comparisons are labeled “UH OH!” because false positives are much more likely to be gotchaz than you might stumble. “” and 0 are definitely markedly different values, and it`s rare that you want to treat them as fair, so their mutual coercion is annoying.