JavaScript Type Casting

You are Here:

JavaScript Type Casting

The Type Casting is a method/technique of changing an entity from one data type to another.

Note: The Type Casting is also known as type conversion, type coercion, and type juggling.

String to Number

In the following example, we will convert a string data type to number data type by using Number() inbuilt JavaScript function.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var x = 1; var y = "2"; var z = x + y; document.write("z = "+z); var z = x + Number(y); document.write("<br>z = "+z); </script> </body> </html>

Number to String

In the following example, we will convert a number data type to string data type by using String() inbuilt JavaScript function.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var x = 1; var y = 2; var z = x + y; document.write("z = "+z); var z = x + String(y); document.write("<br>z = "+z); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author