How to Convert Javascript Variable to Text
JavaScript is the programming language of the Web and is included with all major browsers. Writing code is as simple as writing the code in a text editor and opening it in a browser. JavaScript provides typecasts, which allow the programmer to easily change the type of a variable. For example, changing a number variable to text can be done in less than one line of JavaScript.
Instructions
-
-
1
Open Notepad by clicking "Start" followed by "All Programs," "Accessories" and "Notepad." Start a script by entering a script tag like so:
<script>
-
2
Type the following code into Notepad:
var n=30;
This creates a variable "n" with the numeric value of "30."
-
-
3
Type the following code into your editor:
n = String(n);
This "casts" the "n" variable to a string, or text. This function can be used on all kinds of variables.
-
4
Add the following code, and then close the script tag like so:
document.write(n);
</script>
-
5
Save the document as an ".html" file and open it in a browser. The value of "n" will be displayed.
-
1
Tips & Warnings
Instead of using a text editor, an online JavaScript interpreter may be used.