Home > Web Front-end > JS Tutorial > body text

Making the weakly typed language JavaScript strong_javascript tips

WBOY
Release: 2016-05-16 18:51:13
Original
768 people have browsed it

Weakly typed Javascript will not convert from the actual variable type to the required data type as the programmer wishes. For example, a very common mistake is to get a user input from a form control in a browser script. The sum of a numeric variable and another numeric variable. Because the variable type in the form control is a string (timed string sequence contains a number) this attempt will add that string to the variable, even if the values ​​happen to be some Number, the result will be converted to string type in the second variable, and in the end, only the variable obtained from the form control will be added to the end of the first string.

So forced type conversion is still relatively important. Let’s take a look at several of its forced conversion functions:

1. Boolean(value): Convert the value to Boolean type;

2. Nnumber(value): Convert the value into a number (integer or floating point number);

3. String(value): Convert the value into a string.

Let’s look at Boolean() first: When the value to be converted is "a string of at least one character", "a non-zero number" or an "object", then Boolean() will return true. If the value to be converted is If it is "empty string", "number 0", "undefined", "null", then Boolean() will return false. You can use the following code to test

以下为引用的内容:

  var t1 = Boolean("");//返回false,空字符串
var t2 = Boolean("s");//返回true,非空字符串
var t3 = Boolean(0);//返回false,数字0
var t3 = Boolean(1),t4 = Boolean(-1);//返回true,非0数字
var t5 = Boolean(null),t6 = Boolean(undefined);//返回false
var t7 = Boolean(new Object());//返回true,对象

The following is the quoted content:
var t1 = Boolean("");//Return false, empty string

var t2 = Boolean("s");//Return true, non-empty string

以下为引用的内容:

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
Number(123)  123

var t3 = Boolean( 0);//Return false, number 0var t3 = Boolean(1),t4 = Boolean(-1);//Return true, non-0 number

var t5 = Boolean(null),t6 = Boolean(undefined);//Return false

var t7 = Boolean(new Object());//Return true, object

以下为引用的内容:

  var t1 = null;
var t2 = String(t1);//t2的值 "null"
var t3 = t1.toString();//这里会报错
var t4;
var t5 = String(t4);//t5的值 "undefined

Let’s look at Number() again: Number() is similar to parseInt() and parseFloat(). The difference is that Number() converts the entire value, while parseInt() and parseFloat() can only convert the beginning number part, for example :Number("1.2.3"),Number("123abc") will return NaN, while parseInt("1.2.3") returns 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 completely converted, and then determine whether to call parseInt() or parseFloat(). The following lists the results of calling Number() on some values: TR>
The following is the quoted content: Number(false) 0Number(true) 1Number(undefined) NaNNumber(null) 0Number("1.2") 1.2 Number("12") 12Number("1.2.3") NaNNumber(new Object()) NaNNumber(123) 123
Finally, 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 following is the quoted content: var t1 = null;var t2 = String(t1);//The value of t2 is "null"var t3 = t1.toString();//An error will be reported herevar t4; var t5 = String(t4);//The value of t5 "undefined
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template