The three forced type conversions are: 1. Conversion to numeric type, Number(), parseInt(), parseFloat(); 2. Conversion to string type, toString(), String(); 3. Convert to Boolean type, Boolean().
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript is a weakly typed language, and its value data type can be changed.
Some languages require specifying the data type of each variable. Such a language is called a strongly typed language, such as java C
Forced type conversion
There are two types of type conversion: one One is explicit conversion, which requires programmers to manually write code conversion; the other is implicit conversion, which is converted by the JavaScript interpreter.
JavaScript provides the following functions for explicit conversion:
1. Convert to numeric type: Number(mix), parseInt(string,radix), parseFloat(string)
2. Convert to string type: toString(radix), String(mix)
3. Convert to Boolean type: Boolean(mix)
Convert to numeric type
Number(mix) function
Number(mix) function can convert any type of parameter mix into a numerical type. The rules are:
1. If it is a Boolean value, true and false are converted to 1 and 0 respectively
2. If it is a numeric value, it returns itself.
3. If it is null, return 0
4. If it is undefined, return NaN.
5. If it is a string, follow the following rules:
a. If the string only contains numbers, convert it to decimal (ignoring the leading 0)
b. If the string contains a valid floating point format, convert it to a floating point value (ignoring the leading 0)
c. If it is an empty string, convert it to 0
d. If the string contains a format other than the above, convert it to NaN
6. If it is an object, call the valueOf() method of the object, and then convert the returned value according to the previous rules. If the result of the conversion is NaN, call the object's toString() method and convert the returned string value again according to the previous rules.
Example
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>数据类型转换</title> </head> <body> <script> println(Number(false)); println(Number(true)); println(Number(123)); println(Number(null)); println(Number(undefined)); println(Number(person)); function println(a){ document.write(a+'<br>'); } var person={};//字面量创建对象 // 设置字面量对象属性 person.name='huangshiren'; person.age=58; person.appetite=3; person.eat=function(){ document.write('正在吃饭'); } </script> </body> </html>
parseInt(string, radix) function
Parameter description:
string The value to be parsed. If the argument is not a string, it is converted to a string (using the ToString abstract operation). Whitespace characters at the beginning of the string will be ignored.
radix An integer between 2 and 36 (the basis of mathematical systems) that represents the radix of the above string. For example, the parameter "10" means using the decimal numerical system we usually use. Always specifying this parameter eliminates confusion when reading this code and ensures that the conversion results are predictable. Different implementations produce different results when the base is not specified, usually defaulting to 10.
parseInt(string, radix) function converts a string into an integer type value. It also has certain rules:
1. Ignore spaces in front of the string until the first non-empty character is found
2. If the first character is not a numeric sign or a negative sign, return NaN
3. If the first character is a number, continue parsing until the string is parsed or a non-numeric symbol is encountered.
4. If the result of the previous step starts with 0 , it will be parsed as octal; if it starts with x, it will be parsed as hexadecimal
5. If the radix parameter is specified, radix will be used as the base for parsing
Example
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>数据类型转换</title> </head> <body> <script> println(parseInt("12.34567",10)); println(parseInt("de",10)); println(parseInt("0123456789",10)); println(parseInt("0e0",16)); println(parseInt("0101010001",2)); function println(a){ document.write(a+'<br>'); } </script> </body> </html>
parseFloat(string) function
parseFloat(string) function converts a string into a floating point type value.
Its rules are basically the same as parseInt, but there are some differences: the first decimal point symbol in the string is valid, and parseFloat will ignore all leading 0s. If the string contains a number that can be parsed as an integer , returns an integer value instead of a floating point value.
Example
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>数据类型转换</title> </head> <body> <script> println(parseFloat("3.14")); println(parseFloat("314e-2")); println(parseFloat("0.0314E+2")); println(parseFloat("3.14more non-digit characters")); function println(a){ document.write(a+'<br>'); } </script> </body> </html>
Convert to string type
toString(radix) method. All types of values except undefined and null have a toString() method, which returns a string representation of the object.
Example
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>数据类型转换</title> </head> <body> <script> var count=10; println(count.toString()); var boolean=true; println(boolean.toString()); var today=new Date(); println(today.toString()); var shoppingCart=['鞋','连衣裙','皮带']; println(shoppingCart.toString()); var person={};//字面量创建对象 // 设置字面量对象属性 person.name='huangshiren'; person.age=58; person.appetite=3; person.eat=function(){ document.write('正在吃饭'); } println(person.toString()); function println(a){ document.write(a+'<br>'); } </script> </body> </html>
Convert to Boolean type
Boolean(mix) function converts any type of value to a Boolean value.
Example
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>数据类型转换</title> </head> <body> <script> var count=10; println(Boolean(count)); println(Boolean(0)); println(Boolean(4<3)); println(Boolean(null)); println(Boolean("")); println(Boolean(undefined)); var shoppingCart=['鞋','连衣裙','皮带']; println(Boolean(shoppingCart)); var person={};//字面量创建对象 // 设置字面量对象属性 person.name='huangshiren'; person.age=58; person.appetite=3; person.eat=function(){ document.write('正在吃饭'); } println(Boolean(person)); function println(a){ document.write(a+'<br>'); } </script> </body> </html>
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of What are the three types of casts in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
