About the forced type conversion function in JavaScript

Source: Internet
Author: User

We all know that no matter what programming language we useForced type Conversion FunctionEach function can forcibly convert an expression to a specific data type. The following describesJavascriptForced type conversion function in.

Javascript (ECMA Script) 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 an 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:

 
 
  1. Var t1 = Boolean (""); // return false, empty string
  2. Var t2 = Boolean ("s"); // return true, non-null string
  3. Var t3 = Boolean (0); // return false, number 0
  4. Var t3 = Boolean (1), t4 = Boolean (-1); // return true, not 0
  5. Var t5 = Boolean (null), t6 = Boolean (undefined); // return false
  6. 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 () conversion 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:

 
 
  1. Number(false) 0  
  2. Number(true) 1  
  3. Number(undefined) NaN  
  4. Number(null) 0  
  5. Number("1.2") 1.2  
  6. Number("12") 12  
  7. Number("1.2.3") NaN  
  8. Number(new Object()) NaN  
  9. Number(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:

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

It is recommended that you take a look at this article, which describes the forced type conversion function in C ++ and the forced type conversion function in C ++. Hope to help you.

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.