Let's take a moment to discuss the concept of coercion. This is an important concept in JavaScript because JavaScript is a dynamically typed language. Now in some programming languages, when you establish a variable, you have to specify the type of variable it is what type of value it contains. If it's a string, you indicate such if it's a number, and there are multiple types of numbers, usually in some of these programming languages that you have to indicate. That's an important part of those languages. That's not true for JavaScript.
JavaScript is dynamically typed. So it based upon the value you assigned to that variable. It determines what type it is. Well, because of the nature of this, sometimes coercion happens within JavaScript. I gave an example of this in a previous movie, but let's let's repeat that example or a similar example. So I'm going to open up the console.
I'm going to create A variable set equal to one, create another variable and set that equal to the string two. Now, when we add these together, what will the result be? Well, it comes out as a 12. And notice it's not the number 12. It's a string. And so what JavaScript did is it noticed that you are trying to combine a string and a number using the plus operator, the concatenation operator, depending on what type of value you are working with.
It has to determine what to do because it is dynamically typed. It determines Well, I need to make a best guess and see what needs to be done. In other languages, this would just produce an air not JavaScript and that can sometimes cause problems. So what it did is it converted or forced the number from the type of number to the type of string, and then I concatenated the two together. And that's why we end up with a 12. Now, why is this concept important?
Well, it's important because you may be required to the bug in issue that could be caused by coercion. So this is an element of the JavaScript language that the the dynamically typed variable allows JavaScript or helps JavaScript to make it easier to learn. However, it can also cause problems. And so understanding that about the language can help you solve problems when you're trying to debug code.