Javascript forced type conversion function _ javascript skills

Source: Internet
Author: User
Javascript is a weak type language, so forced type conversion is more important. Let's take a look at several of its forced conversion functions. 1. Boolean (value): Convert the value to the Boolean type;
2. Nnumber (value): converts a value to a number (integer or floating point number );
3. String (value): converts a value to a String.
Let's first look at Boolean (): If the value to be converted is a string of at least one character, a number other than 0, or an object, then Boolean () returns true, if the values to be converted are "null String", "number 0", "undefined", and "null", Boolean () returns false. You can use the following code to test

The Code is as follows:


Var t1 = Boolean (""); // return false, empty string
Var t2 = Boolean ("s"); // return true, non-null string
Var t3 = Boolean (0); // return false, number 0
Var t3 = Boolean (1), t4 = Boolean (-1); // return true, not 0
Var t5 = Boolean (null), t6 = Boolean (undefined); // return false
Var t7 = Boolean (new Object (); // return true, Object


Let's take a look at Number (): Number () is similar to parseInt () and parseFloat (). The difference is that Number () is the entire value, while parseInt () and parseFloat () you can only convert the starting part of the Number. For example, Number ("1.2.3"), Number ("123abc") returns NaN, and parseInt ("1.2.3 ") 1. parseInt ("123abc") returns 123, parseFloat ("1.2.3") returns 1.2, parseFloat ("123abc") returns 123. Number () will first determine whether the value to be converted can be fully converted, and then determine whether it is to call parseInt () or parseFloat (). The following lists the results of calling Number:
Number (false) 0
Number (true) 1
Number (undefined) NaN
Number (null) 0
Number ("1.2") 1.2
Number ("12") 12
Number ("1.2.3") NaN
Number (new Object () NaN
No. (123) 123
String (): This is relatively simple. It can convert all types of data into strings, such as: String (false) --- "false", String (1) --- "1 ". It is somewhat different from the toString () method. The difference is:

The Code is as follows:


Var t1 = null;
Var t2 = String (t1); // The value of t2 "null"
Var t3 = t1.toString (); // an error is reported here.
Var t4;
Var t5 = String (t4); // t5 value "undefined"
Var t6 = t4.toString (); // an error is reported here.

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.