|
Each language has its own basic types, JavaScript is no exception. In JavaScript there are five basic types are number, string, boolean, null, undefined. The other does not belong to the five basic types of objects are sometimes also h is a null object.
First, the basic type of presentation
It can be determined by what type of typeof.
number includes basic digital, Infinity, unInfinity, NaN. Wherein NaN rather special, can not be done in one operation, when do you get a result NaN operation, for example, 1 * 'abc'.
Almost all of the string type of language has, but the implementation is different.
boolean is very common base type, only two values true and false.
null is actually empty
undefined undefined refers
Second, the type conversion
Any language can type conversion. Special thing javascript her back to do some automatic type conversions, benefits of doing so is that developers do not have to do some type of forced conversion, but this feature is also regarded as one of javascript criticized, many people think that this feature can cause some unexpected mistake. Of course, I think once you have a good type conversion features and details, you can while avoiding disadvantages.
Some say in the type conversion error-prone place.
In making the + operator must pay attention because in javascript, + either a digital adder, can also be a string concatenation. When one of the two operands is another numeric string will be converted to a string, then a string concatenation, for example '12 + 1 'and' 1 '+ 12 is obtained string' 112 ' . So if you want to do digital addition, we must ensure that both operands are numbers, you can use Number mandatory conversion, such as 1 + Number ('12 '). Of course, you can use some simple conversions such as 1 + 1 * '12 '. However, these two approaches are not recommended, because semantics is not clear, who will read the code caused confusion or misunderstanding.
Any variable can be converted to a Boolean value, not true is false. Common empty string '', undefined, null, NaN, 0 can be converted to false. Others are all true.
When we define the variable, often we do var a = a || 10. We want to do is, if a is defined using a value is not defined but often ignored assigned to 10. If a value of zero. Therefore, this definition method must be used with caution.
In javascript to do other more when there are two approaches, one is another == === corresponding unequal comparisons! = And! ==. === Strict equality is called. The difference is that == === do not type conversions, such as 1 == '1', 1 == true will return true, and 1 === '1', 1 === true then It returns false. So do the other more when you also must be careful.
Third, the summary
As a weakly typed language, javascript type conversion on the one hand to developers to bring a lot of convenience and sought after by the people, on the other hand also have some inexplicable errors people who were abusive. Actually, I think any language is not perfect, but we can do is really master these characteristics may produce errors, while avoiding disadvantages is king. |
|
|
|