UX Design and Development course

Javascript is a weakly typed language

Versus strong typed. "weak" or "dynamic" typing allows developers to create programs quickly that would be rejected by other languages. Dynamic types languages introduce a strong possibility of an unchecked runtime type error - this is the #1 hardest part of JavaScript

How does your language manage x = "5" + 6?

Does it concatenate the two arguments to produce the string "56" (JavaScript, Java)?

Does it convert "5" to a number, and add the two arguments to produce the number 11 (Perl, PHP)?

Does it fail to compile this program or run the code, saying that the two operands have incompatible type (C)?

In this example, JavaScript will concatenate the two arguments to produce the string "56". Try it for yourself. In Google Chrome, open the inspector, click on the Console tab and enter the following:

"5" + 6

Having a little more fun with this example, we will explore the parseInt function. Learn more about this function.

In the Chrome Inspector, enter the following to set the value of the variable foo:

var foo = parseInt("5", 10) + 6

Then to retrieve this value, simply type in foo. It should return the value of 11. But wait, why did this happen? parseInt is a core function of JavaScript that will take the value within the string and convert it to a number based on it's radix. In this example, the radix is 10. The syntax for this is:

parseInt(string, radix);

Learn more about different language types.