JavaScript must Know (four) JS type conversion _javascript skills

Source: Internet
Author: User
Tags numeric value

String and Number Boolean

The JavaScript type is converted to the appropriate type based on the assignment.

var str = "";
Alert (typeof (str));//string
str =;
Alert (typeof (str));//number
var sum = str +//+ programming two numbers added

This one is more obvious and can be computed at a glance. But take a look at the following conversion

var sum = "" + "";
Alert (typeof (sum)); String
alert (sum);// 
var sum = "" +;
Alert (typeof (sum));     String
alert (sum);//
var sum = + "";
Alert (typeof (sum));//string
alert (sum
); var area = "" * "";
Alert (typeof (area)); Number
alert (area);//
var sub = ""-"";
Alert (typeof (sub)); Number
alert (sub);//
var div = ""/"";
Alert (typeof (Div));//number     

Number type if and string type "+", the number will be directly converted to string

The above "+" is more special, if it is-, *,/What will eventually be turned into what type.

var area = "" * "a";
Alert (typeof);//number alert (area);
//nan
var sub = "A"-"";
Alert (typeof (sub));//number
alert (sub);//nan
sub = "a"-;
Alert (typeof (sub));//number
alert (sub);//nan
var div = ""/"a";
Alert (typeof (Div));//number
alert (div);//nan
div = "a"/;
Alert (typeof (Div));//number

As above-, *,/is the arithmetic in number. String and number are not operational, so their values are Nan. Type number.

var a = true;
Alert (typeof (a));//boolean
var b = "true";
var ab = a + b;
Alert (typeof (AB));//string
alert (AB);//truetrue

Boolean and String, the Boolean type automatically turns to the string "true", but why is a not equal to B?

Let's look at this example:

var c = "";
Alert (typeof (c));//string
var d =;
Alert (typeof (d));//number

The principle of conversion is given here: (for reference)

1. If one operand is a Boolean value, convert it to a numeric value ———— false to a 0,true conversion to 1 before the comparison is equal;
2. If one operand is a string and the other operator is a numeric value, the string is converted to a numeric value before the comparison is equal;
3. If one operand is an object and the other operand is not, the valueof () method of the object is invoked, with the resulting base type value compared to the preceding rule.

Then the comparison of strings and Boolean types would be:

This conversion occurs: Boolean true turns to number 1, and is compared to the string "1". The result must have been false.

Null and string number Boolean undefined

var a = null;
Alert (typeof (a));//object
var b = "Hello";
var ab = a + b;
Alert (typeof (AB));//string
alert (AB);//nullhello
var c =;
var ac = a * C;
Alert (typeof (AC)); Number
alert (AC);//
if (a)//false
{
} else
{
alert ("false");
}
var u;

From the examples given, we can see that:

Null is automatically converted to string "null" in string, and number 0, which is equivalent to false in logical judgment, and is the same as undefined when the value is represented. Note that = = rather than = =.

While JavaScript is comparing, = = converts its comparison type, but its variable type does not change because of = =.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.