By: Emiley J. in Javascript Tutorials on 2008年08月15日 [フレーム]
It's also possible to convert values using a process called type casting. Type casting allows you to access a specific value as if it were of a different type. Three type casts are available in JavaScript:
Boolean(value) – casts the given value as a Boolean
Number(value) – casts the given value as a number (either integer or floating-point)
String(value) – casts the given value a string
Casting a value using one of these three functions creates a new value that is a direct conversion of the original. This can lead to some unexpected results. The Boolean() type cast returns true when the value is a string with at least one character, a number other than 0, or an object (discussed in the next section); it returns false when the value is an empty string, the number 0, undefined , or null . The following code snippet can be used to test type casting as a Boolean:
var b1 = Boolean(""); //false – empty string
var b2 = Boolean("hi"); //true – non-empty string
var b3 = Boolean(100); //true – non-zero number
var b4 = Boolean(null); //false - null
var b5 = Boolean(0); //false - zero
var b6 = Boolean(new Object()); //true – object
The Number() type cast works in a manner similar to parseInt() and parseFloat() , except that it converts the entire value, not just part of it. Remember that parseInt() and parseFloat() only convert up to the first invalid character (in strings), so "4.5.6" becomes "4.5" . Using the Number() type cast, "4.5.6" becomes NaN because the entire string value cannot be converted into a number. If a string value can be converted entirely, Number() decides whether to use parseInt() or parseFloat() . The following table illustrates what happens when Number() is used on various values:
Usage Result
Number(false) 0
Number(true) 1
Number(undefined) NaN
Number(null) 0
Number("5.5") 5.5
Number("56") 56
Number("5.6.7") NaN
Number(new Object()) NaN
Number(100) 100
The last type cast, String() , is the simplest because it can accurately convert any value to a string value. To execute the type cast, it simply calls the toString() method of the value that was passed in, which converts 1 to "1", true to "true", false to "false", and so on. The only difference between type casting as a string and using toString() is that the type cast can produce a string for a null or undefined value without error:
var s1 = String(null); //"null"
var oNull = null;
var s2 = oNull.toString(); //won’t work, causes an error
Type casting is very helpful when dealing with the loosely typed nature of JavaScript, although you should ensure that only proper values are used.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Javascript )
Dynamically modify the option set in Dynamics 365 forms
promise and .then() in JavaScript
reduce() and filter() in JavaScript
History and evolution of Javascript
Using parseInt() and parseFloat() in JavaScript to convert data types to Numbers
Show how many characters remaining in a html text box using javascript
Latest Articles (in Javascript)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate