Let's start with a short entry to type systems that I think will help you understand the general idea of tick compulsion.
The language type system defines rules that tell us which data types exist in that language and how they can be combined using different operators. For example, one of these rules may indicate that the plus (+) operator acts only on numbers. These rules exist primarily so that you do not shoot in the leg. But what happens when a programmer breaks this rule in a program? Theres nothing stopping the programmer from typing {} + {} or "hello" + 5 in the program, even if the language does not think that these expressions make any sense.
What ultimately happens in these situations depends on how strict the language is with its type rules.
The language type system often contains one of two positions that you violate its rules:
- Say "hey it's not cool!" and immediately crash your program.
- Say: "I can’t do anything with {} ... but I can do something with numbers" and try to convert {} to a number.
Languages with type systems, which occupy the first position about their rules, are colloquially called "strongly typed" languages. They are strict, not allowing you to break his rules. Those that take the second approach (such as JavaScript) are called "weakly typed" or "weakly typed" languages. Of course, you can break the rules, but do not be surprised when it converts the type of data that you described in your program, in order to comply with its rules. This behavior is known as ... (drum roll) ... a type of coercion .
Now let's look at some examples in JavaScript. First, let's start with an expression that does not lead to a type of coercion.
5 + 5
Using the + operator with two numbers, which are perfectly valid. The program will relate + to the value "add" and happily add two numbers. No conversion required.
But what about ...
[] + 5
Oh oh In JavaScript + can mean adding two numbers or combining two lines. In this case, we do not have two numbers or two lines. We have only one number and an object. According to rules such as JavaScript, this makes no logical sense. Since his forgiveness that you are breaking his rules, instead of crumbling, he is still trying to figure it out. So what does javascript do? Well, he knows how to concatenate strings, so he converts both [] and 5 to strings, and the result is the string value "5".
What is the deal with == and === comparison operators? Why are there two comparison operators?
== not protected from JavaScripts type conversion behavior. Expressions such as 5 == "5" will be evaluated as true because JavaScript will try to convert one of them so that it compares the same data type.
In many cases, this is undesirable because you probably want to know if any material you are comparing is of a different type so that you can decide what to do with it. This is where the === operator comes in. When you use === , type conversion will not be. Therefore, the expression 5 === "5" will be evaluated as false.