Making weak types of language JavaScript more powerful _ javascript skills

Source: Internet
Author: User
Javascript (ECMAScript) is a weak language. this does not mean that it does not have a data type, but variables or Javascript Object Attributes do not need a specific type of value to be allocated to it or it always uses the same value. variables in Javascript also support conversion of free types into applicable (or required) content for ease of use. weak Javascript does not convert the actual variable type to the required data type according to the programmer's desire. For example, a very common error occurs in browser scripts, obtain the sum of one numeric variable to be input and another numeric variable from the form control. because the variable type is a string type in the Form Control (the timing string sequence contains a number) This attempt will add that string to the variable, even if these values happen to be numbers, the result is converted to the string type in the second variable. At last, the variable obtained from the form control is added to the end of the first string.

Therefore, forced type conversion is important. Let's take a look at several of its forced conversion functions:

1. Boolean (value): converts a 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

Reference content 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:

Reference content is as follows:

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:

Reference content 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

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.