GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialrepublic.com — JavaScript Type Conversions - Tutorial Republic JAVASCRIPT BASIC
JS Introduction
JS Getting Started
JS Syntax
JS Variables
JS Generating Output
JS Data Types
JS Operators
JS Events
JS Strings
JS Numbers
JS If…Else
JS Switch…Case
JS Arrays
JS Sorting Arrays
JS Loops
JS Functions
JS Objects
JAVASCRIPT & DOM
JS DOM Nodes
JS DOM Selectors
JS DOM Styling
JS DOM Get Set Attributes
JS DOM Manipulation
JS DOM Navigation
JAVASCRIPT & BOM
JAVASCRIPT ADVANCED
JS Date and Time
JS Math Operations
JS Type Conversions
JS Event Listeners
JS Event Propagation
JS Borrowing Methods
JS Hoisting Behavior
JS Closures
JS Strict Mode
JS JSON Parsing
JS Error Handling
JS Regular Expressions
JS Form Validation
JS Cookies
JS AJAX Requests
JS ES6 Features
JAVASCRIPT EXAMPLES
JAVASCRIPT REFERENCE
Advertisements
JavaScript Type ConversionsIn this tutorial you will learn how to convert the data type of a value in JavaScript. Automatic Type ConversionsMost of the time JavaScript automatically converts values from one data type to another when used in expression. For example, in mathematical operations values are automatically converted to numbers. However, the end result is not always what you expected: ExampleTry this code »
There are situations when we need to manually convert a value from one data type to another. JavaScript provides a number of different methods to perform such data type conversion task. In the following sections we will discuss those methods in detail. Converting Values to NumbersThe numeric conversion is usually required when we read the value from a string-based source like a text input, but we expect a number to be entered, or want to treat it as a number. In such situations, you can use the global method ExampleTry this code »
If the string is not a valid number, the result will be ExampleTry this code »
Converting Values to StringsSimilarly, you can use the The following example will show you how to convert a Boolean value to a string. ExampleTry this code »
The ExampleTry this code »
Another technique for converting numbers to strings is using the ExampleTry this code »
Converting Values to BooleanBoolean conversions are also pretty straight forward. You can use the Values that are intuitively empty, like ExampleTry this code »
If you see the example carefully you'll find the Note: In some programming languages (namely PHP) the string "0" is treated as Object to Primitive ConversionsAll the conversions we've seen so far are performed on primitive types (data types that can hold only a single value at a time). But what happens with complex data types such as object, let's see. JavaScript automatically perform object-to-string conversion when we try to print out an object like ExampleTry this code »
You can also perform the object-to-string conversion manually using the ExampleTry this code »
Note: Object-to-Boolean conversion are insignificant, because all objects (including arrays and functions) are true in a boolean context. So there are only string and numeric conversions. Type Conversions Using OperatorsCertain JavaScript operators, such as ExampleTry this code »
We hope you've understood the basics of data type conversions. Please, check out the chapter on JavaScript data types to learn more about the different data types available in JavaScript. Advertisements
Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |